Search This Blog

Thursday 24 May 2012

Create Android "Hello Creative Android" Program

This post will quickly explain to develop a "Hello Creative Android" Program for beginners.

See how to setup your Android Development environment

http://creativeandroidapps.blogspot.in/2012/05/setup-android-development-environment.html

Open Eclipse and go to File->New->Others and select the Android folder. Expand the Android Folder and select the Android Project.
Click Next and give name as "HelloCreativeWorld".
Now Click Next and select the build target as Android [4.0.3].
Now Click Next and give a package name (unique package name is required here)

For example :com.creativeandroidapp.example

Keep rest of the items as default and finish the project. See the Project is created in Package explorer.

Expand the project src directory and open the HelloCreativeWorldActivity.java

This file is created by wizard and is the main activity file for your android application.
(Activity is the application component which is responsible to draw the UI and interact with user. There could be multiple activities in one application.)

The main Activity is defined in AndroidMainfest.xml file for the project

<activity
            android:name=".HelloCreativeWorldActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  </activity>

In HelloCreativeWorld example the activity will show you a message on the screen by following statement
setContentView(R.layout.main);


This method is called in onCreate method;this method is called when system creates the activity.


R.layout.main is an XML view setup in the UI.


R stands for Resources and the map file R.java is automatically generated for all the resources you defined in layout XML files.

R.java is created in gen directory of your project.


Expand the res->layout of your project and open the 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="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>



The Text view will display the text message on the UI screen. The android:text property of the TextView is set to a string defined in res->string.xml.


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloCreativeWorldActivity!</string>
    <string name="app_name">HelloCreativeWorld</string>
</resources>



 Change the highlighted text to Hello Creative World.


 Now you are ready to run your app. Right Click on project and click on Run As -> Android Application.


 The android AVD (Android Virtual Device) will be launched. See the following screen shot showing the output of your first app.




Enjoy the first app as Android Developer.

2 comments:

  1. Its as if you read my thoughts! You appear to understand a great deal relating to this, as if you authored it inside it or something like that. I believe you could use a couple of photos they are driving the content home a little, but apart from that, this really is great blog. An excellent read. I'll certainly return.

    ReplyDelete