Tag Archives: How to implement a Button Click event in Android without onClickListener

How to implement a Button Click event in Android without onClickListener?

For this what you need to do is, just add a property in your XML. Lets see how this is done.

There is a property in the Button tag named ‘onClick’ which has to be give a name first as shown below.

We have two buttons . On first button onClick we will show a toast and on second button’s onClick we will show a promptbox (alertbox).

<Button
 android:id="@+id/btn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here"
 android:textSize="30dip"
 android:onClick="MyClickMethod"
 />

 

The Whole XML layout looks like this:

<?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/btn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here"
 android:textSize="30dip"
 android:onClick="MyClickMethod" />

 <Button android:id="@+id/btn2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here for pop up!!"
 android:textSize="30dip"
 android:onClick="ShowPopUp" />

</LinearLayout>

 

Now, lets look at the code. Here, we need to create a method with the same name as that of the name we have given as the property name (Here its MyClickMethod & ShowPopUp).

Within the method you can write what you want to do when the button is clicked. Here in my code, I’ve given a toast to be displayed when it is clicked on first button &  on second button’s onClick we will show a promptbox (alertbox).

 

Here is the code, just have a look.

package com.fundoocode.net;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ButtonClickActivity extends Activity
{
	 /** Called when the activity is first created. */
	  @Override
	 public void onCreate(Bundle savedInstanceState) 
	  {
	     super.onCreate(savedInstanceState);
	     setContentView(R.layout.main);

	  }

	 public void MyClickMethod(View v)
	 {
	      Toast.makeText(getApplicationContext(),"You clicked the first Button " , Toast.LENGTH_LONG).show();
	 }

	 public void ShowPopUp(View v)
	 {
		 Promptbox("Yahoo!!", "Prompt demo", ButtonClickActivity.this); 
	 }

		public static void Promptbox(String title, String mymessage, Activity activity) 
		{
			try 
			{
				new AlertDialog.Builder(activity).setMessage(mymessage).setTitle(title).setCancelable(false).setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener() 
		            {

					  public void onClick(DialogInterface dialog, int whichButton) 
					  {

				      }

				    }).show();
			} 
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

}

 

The Out Put Screen looks like this before the button is clicked.

 

After the button is clicked, the screen looks like this.

 

 

Skip to toolbar