JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

How to write a simple Android application?

April 25, 2013 by Krishna Srinivasan Leave a Comment

In my previous article I have explained about how to set up the android environment. This article shows you how you can create a simple Android Application using eclipse. This example assumes that you have already set up the android environment and ready for the development. If you have any questions, please post it in the comments section.

Step1: Open the Eclipse IDE and create an Android App

Select  File->New->Project->Android->Android Project->Next Refer the below screen for more details about the dialog box.
1_Appdetails

Enter the Following Details in the screen:

  1. Project name: MyFirstApp
  2. Application name: Android App
  3. Package name: my.android.app
  4. Create Activity: helloWorld
  5. Min SDK version: 8(Min SDK version should be same as the API level)
  6. Build Target: Android 2.2

After filling up the above details, click on “Finish” button.

Android Project Structure

Now the project MyFristApp is created in your workspace. The project Structure is as below.

2_ProjectStructure

The Activity helloWorld that you have just created is as below.

[java]
package my.android.app;
import android.app.Activity;
import android.os.Bundle;
public class helloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
[/java]

main.xml is the automatically created layout file. The content of main.xml is as below.

[java]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

[/java]

android:text=”@string/hello” here hello contains “Hello World, helloWorld!” string that gets displayed as output. You can store any string in the Textview to get the output of your choice. You can add any number of views to your xml.

Step2: Register the Activity in AndroidManifest.xml

The Default Activity will be automatically registered. The manifest file is as below.

[java]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".helloWorld"
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>
<uses-sdk android:minSdkVersion="8" />
</manifest>
[/java]

Step3: Run the android application

Create the Android Virtual Device to run your app. Select the project Files that you need to run->click on the below highlighted symbol from the toolbar of eclipse IDE.

3_run

In the Run as window select Android Application and click OK.  Now the Eclipse installs your app in the AVD.

4_outputhelloworld

Step4: Getting the desired Output

strings.xml that gets created automatically is as below:

[java]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, helloWorld!</string>
<string name="app_name">Android App</string>
</resources>
[/java]

Change the contents of strings.xml to alter the output go to  res folder in your project structure->values->open strings.xml->click on the “hello” string. Let us now change the contents of  “hello” string to “Hi Welcome to My World!” The contents of new strings.xml is as below.

[java]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi Welcome to My World!</string>
<string name="app_name">Android App</string>
</resources>
[/java]

Run the Project as in Step3. The new output is as below

5_newoutput

Filed Under: Google Android Tagged With: Android

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved