Tuesday, 11 September 2012

Hello World Program in Android


Simple Hello World Program in Android

Step1: Open Eclipse

Step2 : Specify your Work Space

Step3: Now Go to File => Project => Android Project =>Next



Step4:  Give Some Project Name(Here I am Giving HelloWorld)

Step5: Choose Your Target SDK(Here I am Taking Android 4.0.3. You can choose  any one 2.2 or 2.3 like that).


Step 6: After choosing Target SDK,Click on NEXT.
           It will ask Package name,activity name(Class name).
           For package you need to specify like that (android.hello world or anyname.name).If you not specify “ . “, it will show error.Here I am taking package “test.helloworld”.
          Activity name(Class name) taking as a “HelloWorldActivity”.
        Then Click on Finish.


Step7:  Open Negivator in Your eclipse(Window =>Show View => Nevigator
       Now See your Activity File, Layout file.



Step8: Open all the files that I marked.
            Go 2 main.xml. See Here we have taken TextView that means output will show in text view. See bellow how it would look like.


Step9:The question will arises  How it is showing “Hello World,Hello World Activity”.

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
See here android:text ,we are calling @string/hello. This means it calling to string.xml(res/values).
Open string.xml.
<string name="hello">Hello World, HelloWorldActivity!</string>
    <string name="app_name">HelloWorld</string>
Here we are giving string name “hello” that is calling main.xml. So the output will be “Hello World, HelloWorldActivity!”.


Step10:
Xml is basically used for designing the activity. Now this xml file will call in Java File.
Go To “HelloWorldActivity.java” class(src/test/helloworld).
public class HelloWorldActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
See here HelloWorldActivity class extending Activity.For developing any mobile application we need to inherit the methods from Activity.
Observe in “setContentView(R.layout.main);”, here it is calling that xml file.


Step11: (EXECUTION)
Go to Run=> Click on Run => Choose Android Application => Ok

It will take some time to load your activity.
Result:


5 comments:

  1. No XML content. Please add a root view or layout to your document



    how to solve this error ??

    ReplyDelete
    Replies
    1. Check in your Activity(HelloWorldActivity.java) file. setContentView(R.layout.main) will be there.
      When You execute your project, you must Execute from your main java file or RightClick on Project name(Navigatore)=>Run As=> Android Application

      Delete
    2. main.xml file will look like bellow:



      Delete
  2. try to insert the google followers and fb like option in the blog to follow the blog regularly

    ReplyDelete
    Replies
    1. Thanks For Your Suggestion.......
      I will Do It.
      My facebook Page:
      http://www.facebook.com/Android4Delopment

      Delete