This is the first part of the android application tutorial. This application aims to provide an way to show the message when the button clicks.I created the eclipse project and set up a basic interface for the main activity of the application.

In web technology we displayed a message using java script or any other programming concepts. In android we are using onclick listener for making to display message.

setOnClickListener(new View.OnClickListener() 

In android toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out, most people just use it for debugging purpose.

code snippets to create a Toast message :
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();

//display in long period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();

when a user clicks the button, the android system calls the activity onclick(view) method. The method must be public and accept a view as its only parameter.

public class Toast1Activity extends Activity 
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() 
{  
public void onClick(View arg0) 
{ Toast.makeText(getBaseContext(), "you clicked me", Toast.LENGTH_LONG).show(); }
});
}
}

 

XML layout


Instead of applying an onclicklistener to the button in your activity, you can assign a method to your button in the xml layout using android:onclick attribute.

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="button" />

when a user clicks the button, the android system calls the activity onclick(view) method. The method must be public and accept a view as its only parameter.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button" />
</LinearLayout>

 

AndroidManifest


This manifest.xml file presents essential information about the application to the android system.It names the java packages for the application. The package name serves as a unique identifier for the application. This is the basic application for andrid beginners. It helps to implement different application.

Final Output:




Download Code


Categories: