Hello World with EditText input
In this exercise, I will implement a simply Hello World Andrid application; there is a EditText, after user enter name and click OK button, the Title of the application will be changed accordingly.
create new project and give Name of it,as shown in Previous exercise.
Modify the main.xml to change the UI screen:
main.xml
<?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"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Who you are?"
/>
<EditText
android:id = "@+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id = "@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id = "@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
And then, change the HelloAndroid.java as follow:
HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(okOnClickListener);
Button cancelButton = (Button) findViewById(R.id.cancel);
cancelButton.setOnClickListener(cancelOnClickListener);
}
private Button.OnClickListener okOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View v) {
EditText edit_text = (EditText)
findViewById(R.id.message_text);
CharSequence edit_text_value =
edit_text.getText();
setTitle("Hello:"+edit_text_value);
}
};
private Button.OnClickListener cancelOnClickListener =
new Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};
}
now Its time to run this package >>>
Running on the emulator
Before you can run your application on the Android Emulator, you must create an AVD.
The AVD Manager is an easy to use user interface to manage your AVD (Android Virtual Device) configurations. An AVD is a device configuration for the Android emulator that allows you to model different configurations of Android-powered devices. When you start the AVD Manager in Eclipse or navigate to your SDK's
tools/
directory and execute
android avd
. You will see the AVD Manager as shown below in
figure.
Creating AVD:
You can create as many AVDs as you would like to test on. It is recommended that you test your
applications on all API levels higher than the target API level for your application.To create an AVD:
- Start the AVD Manager:
- In Eclipse: select Window > AVD Manager, or click the AVD Manager icon in the Eclipse toolbar.
- In other IDEs: Navigate to your SDK's
tools/
directory and execute theandroid
tool with no arguments.
- In the Virtual Devices panel, you'll see a list of existing AVDs. Click
New to create a new AVD. The Create New AVD dialog appears.
- Fill in the details for the AVD.
Give it a name, a platform target, an SD card size, and a skin (HVGA is default). You can
also add specific hardware features of the emulated device by clicking the
New... button and selecting the feature. For a list of hardware features.
Note: Be sure to define a target for your AVD that satisfies your application's Build Target (the AVD platform target must have an API Level equal to or greater than the API Level that your application compiles against). - Click Create AVD.
- Fill in the details for the AVD.
Give it a name, a platform target, an SD card size, and a skin (HVGA is default). You can
also add specific hardware features of the emulated device by clicking the
New... button and selecting the feature. For a list of hardware features.
- Compile the project (if there have been changes since the last build).
- Create a default run configuration (if one does not already exist for the project).
- Install and start the application on an emulator (or device), based on the Deployment
Target defined by the run configuration. If you run the application with the Debug option, the application will start in the "Waiting For Debugger" mode. Once the debugger is attached, Eclipse opens the Debug perspective and starts the application's main activity. Otherwise, if you run the application with the normal Run option, Eclipse installs the application on the device and launches the main activity.Your AVD is now ready and you can either close the AVD Manager, create more AVDs, or launch an emulator with the AVD by selecting a device and clicking Start.
- then Right click on your project (@project explorer)
and then click on RUN AS -> ANDROID APPLICATION ...It will be appear after a few time in the AVD which you have created.