In this post i am going to explain how to create an android application that contain some simple user interface. Here i just create a button interface and when the user click the button a message appear in a Toast. A toast is a simple user interface that shows some information to the user and close automatically after some time.
Create a new project in the eclipse ide and name it as shown bellow.
Click the image to view full size |
I name the xml file as first_layout and the java class as First.java. In the layout file i just create a button interface by using the following xml tags.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayMessage"
android:text="@string/button_name" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayMessage"
android:text="@string/button_name" />
In the layout file, i just add an android button interface. Here i just create a method called "displayMessage" which is invoked when the user click the button.
android:onClick="displayMessage"
Here the label of the button is referenced from the strings.xml file using the following xml tag.
android:text="@string/button_name"
Here you have to specify the following entry in the strings.xml file.
<string name="button_name">Show Message</string>
Now i have to write the method displyMessage in the First.java file as shown bellow.
public void displayMessage(View v)
{
Toast.makeText(this,"YOU JUST CLICK THE BUTTON", Toast.LENGTH_LONG).show();
}
Toast.makeText(this,"YOU JUST CLICK THE BUTTON", Toast.LENGTH_LONG).show();
}
Make sure that the method receives an object of View as argument.
Here is the compleate code of this application.
first_layout.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".First" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayMessage"
android:text="@string/button_name" />
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".First" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayMessage"
android:text="@string/button_name" />
</RelativeLayout>
First.java
package com.easyway2in;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class First extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
public void displayMessage(View v)
{
Toast.makeText(this,"YOU JUST CLICK THE BUTTON", Toast.LENGTH_LONG).show();
}
}
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class First extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
public void displayMessage(View v)
{
Toast.makeText(this,"YOU JUST CLICK THE BUTTON", Toast.LENGTH_LONG).show();
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources>
<string name="app_name">ViewApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="button_name">Show Message</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="button_name">Show Message</string>
</resources>