Thursday, 9 August 2012

HOW TO CREATE A SPLASH SCREEN IN ANDROID APPLICATION

Step 1:

Create a project in Eclipse
Project name : SplashScreen,
Application Name : SplashScreen,
Package Name : com.myapps.splashscreen ,
Create Activity : Leave it as it is,
AVD : 2.3 gingerbread,

Beginners : Refer Here...

After creating a project, Here’s what your package should look like.
Figure 1:



STEP 2:

Next we have to add one more activity in our package, so that we can make a transition from splash screen to main page(Activity).
So..
Add a new Activity named WelcomeActivity and create a new layout XML file named welcome.xml.

Then configure the SplashActivity to use the main.xml file for it’s layout and the WelcomeActivity to use welcome.xml.

So here i ill show how to do that..

Adding WelcomeActivity.java

Right Click the package com.myapps.splash -> New->Class..
Name : WelcomeActivity ,
Then, Click Browse as shown below.

Figure 2

Choose a Type : android.app.Activity (Simply type 'Activity' in the box, and you will get many list of options below, click on Activity). 
Click OK.
Click Finish.

Now you can see WelcomeActivity.java  added in the package.

Lets add the welcome.xml file (UI for WelcomeActivity.java).

Adding welcome.xml

 Open res Folder, Right click on Layout Folder -> New-> Android XML File..

 Figure 3:

Type in,
File : welcome (all in small letters),
Click Finish.

Figure 4:
You would Probably get a screen like this, this is nothing but a graphical view of your UI, click on the welcome.xml tab for xml view.


Next Step..

Step 3:

Configure the WelcomeActivity to use the welcome.xml file for it’s layout.
open WelcomeActivity.java,

Your code should look like this,
package com.myapp.splash;

import android.app.Activity;
import android.os.Bundle;

public class WelcomeActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
   setContentView(R.layout.welcome);
 }
}

 
Your class must extend 'Activity', (refer Activity Here).

if your code doesn't have oncreate method then, just after the opening parenthesis type oncreate and hold ctrl and press space bar (ctrl+spacebar)
you will get a screen like this

Figure 5:

Click on the 1st Oncreate(bundle).
next..
After "super.onCreate(savedInstanceState);"

type "setContentView(R.layout.welcome);"

& save it.


STEP 4:

Next ,
Setting up the xml for UI purpose..
Go to layout->and open the main.xml..go to XML Layout.
And copy paste the below xml

main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" android:layout_gravity="center_vertical|center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_vertical|center"/>

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_vertical|center"/>
    
            <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Development"
        android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_vertical|center"/>
        
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/>

</LinearLayout>

Figure 6:
main.xml will look like this.

Similiarly do it for welcome.xml

welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="WELCOME TO MAIN ACTIVITY"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


STEP 5:

Once your project is set up, open SplashActivity.java and we’ll make a few changes. Add a variable named splashDelay to hold the length of time to keep the Splash Screen up. Then we’ll use a Timer and a TimerTask to help us schedule the transition to WelcomeActivity. Note that before starting the new Activity we call the finish() method on the SplashActivity. This prevents the user from being able to use the back button to return to this Activity.

Open src-> com.myapps.splash-> SplashScreenActivity.java file 

SplashScreenActivity.java
package com.myapp.splash;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SlidingDrawer;

public class SplashScreenActivity extends Activity {
    private long splashDelay = 5000; //5 sec's
 
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TimerTask task = new TimerTask() {
   
   @Override
   public void run() {
    finish();
    Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, WelcomeActivity.class);
    startActivity(mainIntent);
    
   }
  };
  Timer timer = new Timer();
  timer.schedule(task,  splashDelay);
    }
}

Copy paste.
if the project requires import..example for Timer Class..
just hold and press Ctrl+Shift+ O for required imports


STEP 6:

Finally, Add entry in android manifest file.
we need to make two changes to AndroidManifest.xml. Set the application element’s theme attribute to Theme.NoTitleBar to get rid of the Title Bar and on the SplashActivity’s activity element, set the screenOrientation attribute to portrait to fix the SplashScreen to portrait layout.

NOTE : Please dont forget to add entry of new class "WelcomeActivity.java"

Open AndroidManifest File and Edit it
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amyapp.splash"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".WelcomeActivity">
            
            
        </activity>
    </application>

</manifest>


Run the project..and you will see result like this
Congrats you made it!! :)
Thankyou
 

Tuesday, 7 August 2012

Android Basic 'Hello World' Example

This tutorial is for Android beginners and will show you how to develop a simple hello world application in android using Eclipse IDE. 

This tutorial assumes that you have everything required for the Android development..if you don't have then go to my tutorial 'Getting started with Android'.

http://shishir4android.blogspot.in/2012/08/getting-started-step-1-download-android.html

 

 STEP 1:

Create Android Project: 

Open Eclipse, select “File -> New -> Project….”, “Android Project” As Shown in Figure 1,

Figure 1:



Next, input your application detail. Eclipse will create all the necessary Android project files and configuration. Click Next
Figure 2:


Figure 3 :   

Select any one Emulator you create earlier, As you can see their are many AVD in my list..but i usually use 'Gingerbread' ie: version 2.3 , Click Next.


Figure 4 :

Type in the details as shown below, Package name can be anything..but usually put a reverse domain name. Click Finish

And you are done..Eclipse will create all the necessary Android project files and configuration.as shown in Figure 5

Figure 5:


Step 2:

HELLO WORLD

Locate the generated activity file, and modify as shown below,

Go to your src->your packages->HelloWorldActivity.java

File : HelloWorldActivity.java

package com.myapps.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        TextView text = new TextView(this);
        text.setText("Hello World, This is Android Basic tutorial");
        setContentView(text);
    }
}

 

 

Step 3:

DEMO

Run the application on Emulator..Right click on project->Run as->Android Application

wait for Emulator to boot up..

Figure 6: 

here is the result..

NOTE: This tutorial is only for the beginners ..it only explains how to start up with android development

Enjoy...

Saturday, 4 August 2012

GETTING STARTED WITH ANDROID

STEP 1 >>

Download Android SDK Latest Version:

Click on the link below to download Latest SDK,

 http://developer.android.com/sdk/index.html

 

Before installing SDK please check the requirement

         Operating Systems

  • Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)
  • Mac OS X 10.5.8 or later (x86 only)
  • Linux (tested on Ubuntu Linux, Lucid Lynx)
    • GNU C Library (glibc) 2.7 or later is required.
    • On Ubuntu Linux, version 8.04 or later is required.
    • 64-bit distributions must be capable of running 32-bit applications.

    Eclipse IDE

  • Eclipse 3.6.2 (Helios) or greater
    Note: Eclipse 3.5 (Galileo) is no longer supported with the latest version of ADT.
  • Eclipse JDT plugin (included in most Eclipse IDE packages)
  • JDK 6 (JRE alone is not sufficient)
  • Android Development Tools plugin (recommended)
  • Not compatible with Gnu Compiler for Java (gcj)

    Other development environments

  • JDK 6 (JRE alone is not sufficient)
  • Apache Ant 1.8 or later
  • Not compatible with Gnu Compiler for Java (gcj)
Note: Some Linux distributions may include JDK 1.4 or Gnu Compiler for Java, both of which are not supported for Android development. 



STEP 2 >>

Adding Platforms and Packages: 

After you have successfully installed the SDK ,

Click Start-> all programs->android SDK tool-> Click SDK manager.. 

When you open the Android SDK Manager, it automatically selects a set of recommended packages. Simply click Install to install the recommended packages. The Android SDK Manager installs the selected packages into your Android SDK environment. The following sections describe some of the available SDK packages and more about which ones we recommend you install.

Download each individual SDK mostly download everything..

Recommended 'Ginger Bread SDK 2.3.3 API 10" 

Once you have installed your packages, continue to the next page. 

Figure 1 : Android SDK Manager.



STEP 3 >>

Adding Emulators:

Open Android Virtual Device manager as shown in Figure 2

 

Figure 2 : Android virtual device manager:


 

 

To Open 'new virtual device' dialog box Click New

Figure 3:



Next fill in all details as above:

NOTE: Snapshot helps to launch emulator without booting it up again.

 

To check everything's fine go to Virtual device -> Click AVD 'Ginger_bread'->Click Start-> launch

Figure 4:

 It takes time to boot up..so be patient..

Here he have done with the SDK and AVD manager..Next Step Installing Eclipse

STEP 4>>

Next Step To download Eclipse:

Download the latest version of eclipse from :

http://www.eclipse.org/downloads/

 

Figure 5:

 

 

 For windows click on window32 bit and download the zip file..extract it to drive you wish and run Eclipse..