Its a longstanding tradition that when learning a new programming language or a framework, the first program written is to display a ‘Hello World’ on screen. Its pretty easy with Android. We can create a Hello World program in Android with practically no coding at all. Here we will make our first Android app with some interesting functionality, rather than just a simple ‘Hello World’ app.
Create a new project
Open Eclipse
Go to File→New→Project
Create the project with following properties:
- Project Name: First App
- Build Target: Select a suitable platform. Here I’ve selected 2.3.3
- Application Name: First App
- Package Name: com.firstapp
- Create Activity: MainActivity
- Min SDK version: 5
Project Name:
Name of your Project. Make it a descriptive one so that you can identify it from others. A directory of that name will be created in the Eclipse workspace which contains the project files.
Build Target:
Here we are specifying under which API level, we are going to develop our program. If we select 2.3, it means that we are developing our app with 2.3 API. We are able to use only the features of 2.3 framework in our app.
Application Name:
The name of our application. This name will be displayed for our application, when installed on a device.
Package Name:
Its the name of the Java package in which our code is going to be organized. It is a method to organize Java classes into modules. ‘com.maindomain.subdomain’ is the common package naming convention.
Create Activity:
This is the name of the first Activity in our application. When our application runs, this activity will be called by the system by default.
Min SDK version:
By Minimum Android version, we are specifying the minimum version of Android that is capable of running our application.
Now click Finish. Our new project is created.
Creating a Run configuration
Go to Run→Run Configurations
Select Android Application in the left pane and click on the New Launch Configuration button as shown in the figure.
Add a suitable name for the new Run Configuration select your current project by clicking on browse.
Select Target tab
Click Automatic and select the target virtual device you’ve created before.
(If you want to debug your application in a hardware device then select Manual instead of Automatic)
Click Apply and Close the dialogue.
Now you’ve successfully created a Run Configuration of your project.
Now Run your app by selecting Run→Run or by clicking the Run button in the toolbar.
If you’ve opted for manual target selection in the Run Configuration setup, Android will present you a chooser from where you can choose your run target device. Choose your virtual device or the physical device and click OK.
Now the emulator will start, it will take some time to load. After loading you will see the lock screen. Drag the slider to unlock the screen. Now you can see your Activity in the screen.
Without writing even a line of code, we made our Hello World application. It is really amazing, isn’t it. Lets analyze those components in our project that made this happen.
Go to Package Explorer window in the Eclipse and open AndroidManifest.xml file in the root folder.
Open the AndroidMAnifest.xml tab in the bottom of the window. You will see the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| <?xml version="1.0" encoding="utf-8"?> package="com.firstapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="5" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest> |
The AndroidManifest file is a road map for the Android OS, when your app starts executing. It tells the OS, details of your app. It contains all informations about your app such version name, application label, icon file, user permissions required, details about activities and services etc.
The first line is the XML version and encoding declaration. Next it specify the package name, version code and version name of the app. Then name of the application and the icon file is pointed. Next is the name and label of the Activity. Intent filter specifies that this Activity should start when the applcation is launched.
Now open MainActivity.java in the SRC folder under the package name. It will look like the code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
| package com.firstapp;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }} |
Let us see how the UI of our application is defined. Open main.xml under res/layout folder.
By default you will see the graphical layout. Switch it to text layout by clicking on the main.xml tab at the bottom.
Heres the code for main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
| <?xml version="1.0" encoding="utf-8"?> 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> |
Following code shows how string resources are defined in Android.
1
2
3
4
5
6
7
| <?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">First App</string></resources> |
String named hello is used for the TextView and app_name is used in the manifest file as label of application.
So, this is how a ‘Hello World’ program in Android can be made. Now we are going to make some changes in these to build our first app.
First open the main.xml file and make the following changes.
Add an id to the LinearLayout and TextView widget so that we can programmaticaly refer it in Java.
1
2
3
4
5
6
7
8
9
10
| android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/background" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/text1" /> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/test" android:text="@string/button_test" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/changecolor" android:text="@string/button_color" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/exit" android:text="@string/button_exit" /> |
Add these strings:
1
2
3
| <string name="button_test">Test</string> <string name="button_color">Change Background</string> <string name="button_exit">Exit</string> |
Add a color file to the values folder and define a color
For this, right click on the values folder
Select New→File
Enter filename as colors.xml and click Finish
Enter this xml code in colors.xml and Save.
1
2
3
4
5
6
| <?xml version="1.0" encoding="utf-8"?><resources> <color name="newcolor">#121289</color></resources> |
There are several ways by which we can handle a click event in Android. Now we will use the simplest method.
Open layout.xml and add this code as attribute of each of the <Button>tag.
1
| android:onClick="newClickHandler" |
We should define the newClickHandler() method in MainActivity.java.
Open MainActivity.java and add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| public void newClickHandler(View view){ TextView text1=(TextView)findViewById(R.id.text1); LinearLayout background=(LinearLayout)findViewById(R.id.background); int s=view.getId(); switch(s){ case R.id.test: text1.setText("Its Working"); break; case R.id.changecolor: background.setBackgroundResource(R.color.newcolor); break; case R.id.exit: finish(); break;} |
Entering the above code will result in some error messages in Eclipse. This is because, to use LinearLayout, TextView, View etc. In our program, we need to import some packages at the beginning. Hovering on the error sign will tell you some packages are required to be imported. Click on it and you will get the option to import those packages. These are the packages that are required:
1
2
3
| import android.view.View;import android.widget.LinearLayout;import android.widget.TextView; |
Congrats. We’ve successfully completed our first Android app. From this you’ve a basic idea on User Interface creation, resource creation and management, event handling etc. You can copy the final code given below.
Final Code:
/res/layout/main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| <?xml version="1.0" encoding="utf-8"?> android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/background" >" <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/text1" /><strong>/res/values/strings.xml</strong> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/test" android:onClick="newClickHandler" android:text="@string/button_test" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/changecolor" android:onClick="newClickHandler" android:text="@string/button_color" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/exit" android:onClick="newClickHandler" android:text="@string/button_exit" />"</LinearLayout><strong></strong><strong></strong> |
1
2
3
4
5
6
7
8
9
| <?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">First App</string> <string name="button_test">Test</string> <string name="button_color">Change Background</string> <string name="button_exit">Exit</string></resources> |
1
2
3
4
5
6
| <?xml version="1.0" encoding="utf-8"?><resources> <color name="newcolor">#121289</color></resources> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| package com.firstapp;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void newClickHandler(View view){ TextView text1=(TextView)findViewById(R.id.text1); LinearLayout background=(LinearLayout)findViewById(R.id.background); int s=view.getId(); switch(s){ case R.id.test: text1.setText("Its Working"); break; case R.id.changecolor: background.setBackgroundResource(R.color.newcolor); break; case R.id.exit: finish(); break; } }} |
No comments:
Post a Comment