On Button Click Sample code

Posted: June 7, 2011 in Uncategorized

This sample is for executing click event of button.. you can implement same for any other view also like image view or anything..


test.java (Activity)

 package com.example.test;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 public class test extends Activity implements OnClickListener{
 Button btn;
 int count=0;
 public void onCreate(Bundle icicle)
 btn= (Button)findViewById(R.id.button1);

public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText(“Clicked “+ ++count + ” Times”);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linear1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
<Button android:text="ClickMe" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


end of code



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s