Using the layout to define a click action

Download android eBook

Example

When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks.

Button

<Button
    android:width="120dp"
    android:height="wrap_content"
    android:text="Click me"
    android:onClick="handleClick" />

Then in your activity, create the handleClick method.

public void handleClick(View v) {
    // Do whatever.
}

Stats

Contributors: 2
2016-08-23
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook