Android Button setOnClickListener Example
Nov 04, 2013 Janaki Mahapatra, Android
- Add a button to your "res/layout/main.xml" file.
exmaple:
<?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:text="Click Me" /> </LinearLayout>
- Now attach an event Click Listener to the button.
example:
button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() {/* Some Code */ });
- Here is the complete code for it