This tutorial will guide you to create a simple hello word application in android. Through this tutorial you will learn about the basic understanding of android activity and layout. Most common tool to create android application is Eclipse ADT plugin. You can follow step by step how to install eclipse ADT Plugin in the official developer site here.
After your eclipse ADT plugin was installed, now you’re ready to create android Hello Word project. In eclipse, select menu
File->New->Other
This will open a wizard.
On the wizard, open Android sub tree and choose Android Applicaton Project then click Next.
The wizard will prompt you to fill below informations:
- Application Name: this name will shown in the google play store
- Project Name: name used by eclipse, must be unique in a workspace
- Package Name: this is your application identifier, must be unique for each application.
The next screen will allow you to put icon for your application. Choose your application icon or just click next until you see this window:
Activity in android just like a screen that is visible to the user. For now, just choose Blank Activity.
Now you must set Activity Name and Layout Name. Activity name is a class name for your activity and layout name is your layout file name. Click finish to close the wizard. Now, you will see a HelloWord project on eclipse project explorer. Here is the folder structure of HelloWord project.
Here is basic description about project sub folders:
- src: contains your java source files
- bin: contains your compiled java class
- gen: contains android generated files
- libs: here you can place third party library (jar file)
- res: contains your projects’ resources, like images, icons, layouts, strings, etc
Now open [cci]res/layout/activity_main.xml[/cci] file, it will looks like this:
This file defines your layout structure. You can place your view object here. There are two categories of view in android, [cci]View[/cci] and [cci]ViewGroup[/cci] or container. [cci]View[/cci] object can be [cci]TextView, EditText, Spinner[/cci], etc while [cci]ViewGroup[/cci] acts as the [cci]View[/cci] container.
As you can see from above source code, it uses [cci]RelativeLayout[/cci] as main container. This layout places it’s childs relative to other childs. With this layout you can place any child overlapping to others. Another most common layout is [cci]LinearLayout.[/cci] The main function of [cci]LinearLayout[/cci] is to place their child in orientation manner, vertical or horizontal.
Either [cci]View[/cci] and [cci]ViewGroup[/cci] must have [cci]android:layout_width[/cci] and [cci]android:layout_height[/cci], these attributes respectively defines [cci]View[/cci] width and height. You can put value for both attributes using integer value in [cci]dp[/cci] such as [cci]0dp, 30dp, 100dp[/cci], etc. [cci]dp[/cci] or [cci]dip[/cci] is android unit which means [cci]density independent pixels[/cci]. Also you can put following constant value :
- [cci]MATCH_PARENT: [/cci]make the size of [cci]View[/cci] as big as it’s parent (container) minus padding.
- [cci]WRAP_CONTENT: [/cci]make the size of[cci] View [/cci]to match content size.
Now take a look inside [cci]RelativeLayout[/cci] at above code. There is one TextView inside with an attribute [cci]android:text:”@string/hello_word”[/cci], this attribute defines the text for [cci]TextView[/cci], but as you can see, the attribute value is not direct string but refers to string resources. You can find the string resource in folder [cci]res/values/string.xml[/cci].
HelloWord Settings Hello world!
Android will search in the string resources for string that has name [cci]hello_word[/cci] and then use the value. Actually you can put direct string like this [cci]android:text:”Hello world!”[/cci], but the recommended way is using string resource to put your string.
Now how to tell android to use this layout? Goto [cci]src[/cci] folder, and open [cci]MainActivity.java[/cci],
package com.semurjengkol.helloword; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
To use you layout in activity, you must override [cci]onCreate[/cci] method of the [cci]Activity[/cci] class and then call [cci]setContentView(R.layout.activity_main);[/cci] now you can run your [cci]HelloWord[/cci] application, it’s will looks like this.
Download source code HelloWord