Monday, October 22, 2012

Click from XML in android


Set Button click from xml file in android.

See more update about android here

In the xml file set button property "Onclick".

In the "Onclick property" set argument as a your method name available in java file.

Below the example of set click listner from xml file.

Put this code for button in layout file.

In below code <android:onClick="onBtnClicked">.

Create "onBtnClicked" method in java file.

<Button
android:id="@+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onBtnClicked">
</Button>

java code display below.

public void onBtnClicked(View v){
    if(v.getId() == R.id.my_btn){
        //handle the click here
    }
}

See more update about android here