Powered By Blogger

Monday, July 16, 2012

Android-Simple Program


       Go to File>New>project and select Android project.
       Fill in the desired project name i.e. àhelloandroid. Next.
       The version of android will be checked by default. Next.
       Application name will be same as project name and package name à com.example.helloandroid. Finish.

Virtual emulator settings:
       In Eclipse, select Window > Android SDK and AVD Manager.
       Select Virtual Devices in the left panel.
       Click New....The Create New AVD dialog appears.
       Type the name of the AVD, such as "my_avd".
       Choose a target. The target is the platform (that is, the version of the Android SDK, such as 4.0.3) you want to run on the emulator. For this tutorial, choose the latest platform that you have installed and ignore the rest of the fields.
       Click Create AVD.
       Now project has been created on the left hand side panel you will have the project name folder. Explore it. Goto src>com.example.helloandroid. Open helloandroid.java file.
       It will have the following code.

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class helloandroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Edit it to..

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;  /** textview widget is added to show the text on blank screen*/

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");    /** the tab screen in our case the virtual emulator shows the given text*/
       setContentView(tv);

   }
}

Note: the things in bold letters are only the new lines to previous code. Save and Run the code.
  • A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of the View class and the subclass that handles text is TextView.  
  • you create a TextView with the class constructor, which accepts an Android Context instance as its parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.  
  • SetText will show the string content within the paranthases. And setContentView will show the result on emulator screen that is tv.



No comments:

Post a Comment