Hello World! – First Android App
This tutorial will guide you in creating a very simple first Android app.
It is very easy to build the Application in Android. We had learned in our old tutorials about the basic concepts.
So it’s time to do some coding and create the Hello World Example in Android.
Requirements for creating first android app
Here, we use the Eclipse IDE. Update the SDK tools in SDK manager if not with the latest Android version.
Example with code:
Create an Application by following the steps:
Click on File->New->Android Application Project->HelloWorldExample.
See the figure below for creating a new application:
Provide some name of the application, the package name is created by default. If you want you can change the package name.
After providing the details in above screen, press Next and create an Activity as shown in the figure below:
Here, you can choose anyone. However, for a single Activity, it will choose the first option by default i.e. BlankActivity. Press Next and then you will see the step shown in the figure below:
It shows the name of our Activity, i.e MainActivity, and its layout file. We can change the name of both if want.
Now, after pressing Next it creates the application in your workspace. This can take few minutes (especially if this is the first project you created).
Let’s see and understand some of these files, i.e. MainActivity.java, activity_main.xml and AndroidManifest.xml file.
activity.main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.helloworldexample.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
This XML file is our Main layout file which shows the view on the screen. Here, it contains a TextView with some text inside the RelativeLayout element.
MainActivity.java
This is our MainActivity which sets the view inside onCreate() method.
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Here, we can see very well it takes the reference of our layout. We can add various widgets to show on the screen, just take the reference of all of them in our Activity.
By default, this application set the intent-filter for our MainActivity in AndroidManifest.xml file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworldexample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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>
Here, you can see the version name, code, app name etc. and our MainActivity is registered by default with action and the category tag.
strings.xml
<string name="app_name">HelloWorldExample</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string>
This is our strings file in values folder. Here, we can create the strings and fetch in our whole application.
This is your auto-generated app and now you can compile and run the app from IDE.
Output
Following is the output of our application. You can see it shows a text “Hello World” on screen.
So, this is all about the simple auto-generated application of Android. In future posts, we will learn more about custom apps.