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)
 {
super.onCreate(icicle);
 setContentView(R.layout.main);
 btn= (Button)findViewById(R.id.button1);
 btn.setOnClickListener(this);
 }

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

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

———————————————

end of code

————————————————-

About these ads

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