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.

<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"
 />
</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). 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.

Here is the code, just have a look.

package com.deepthi.mybutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MyButtonWithoutOnClickActivity 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 Button", Toast.LENGTH_LONG).show();
 }
}

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

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

Advertisements

, , , ,

  1. #1 by Adil on March 30, 2013 - 2:52 am

    Simple but very important,.

  2. #2 by Celebs Without makeup on April 19, 2013 - 12:21 pm

    Valuable information. Fortunate me I discovered your web site
    by chance, and I am stunned why this coincidence didn’t happened earlier! I bookmarked it.

  3. #3 by lady gaga on August 5, 2013 - 5:08 am

    Thanks for another informative site. Where else may I get that
    type of info written in such an ideal manner? I have a mission that I am simply now working on,
    and I’ve been at the look out for such information.

  4. #4 by Nett Bitt on October 17, 2013 - 12:42 pm

    Hello,
    how create a button to set wallpaper from internet site gallery?

    Regards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: