Working with Android Buttons

A Button is a Push-button that can be pressed by the user to act. The android.widget.button is a subclass of TextView class and CompoundButton is the subclass of the Button class. The button consists of text or an icon (or both text and an icon) that describes what action occurs when the user clicks it.

Buttons Attributes:

Inherited from android.widget.TextView Class –

  1. android:autoText

If set specifies that this TextView has a textual input method and automatically corrects some common spelling errors.

2. android:drawableBottom

This is drawable to be drawn below the text.

3. android:drawableRight

This is drawable to be drawn to the right of the text.

4. android: editable

If set specifies that this TextView has an input method.

5. android: text

This is the Text to display.

Inherited from android.view.View Class –

  1. android:background

This is drawable to use as the background.

2. android:contentDescription

This defines the text that briefly describes the content of the view.

3. android: id

This supplies an identifier name for this view.

4. android:onClick

This is the name of the method in this View’s context to invoke when the view is clicked.

5. android: visibility

This controls the initial visibility of the view.

We can create a button in the layout file in three ways using text, an icon, or both text and an icon:

  • With text, using the Button class:
  • With an icon, using the ImageButton class:
  • With text and an icon, using the Button class with the android:drawableLeft attribute:

Define Click Events in Button:

1.Using onClick:

When the user clicks a button, the Button object receives an on-click event.

To define it for a button, add the android:onClick attribute to the <Button> element in the XML layout file. The value for the attribute is given by you whatever you want to call from that attribute. The Activity implements the corresponding method.

For example, here’s a layout with a button using android:onClick:

In activity java file, handle the click event:

The method declares in the android:onClick attribute must have that format exactly as shown above. The method must:

  • Be public
  • Return as void
  • Define a View as its only parameter (this will be the View that was clicked)

2.Using OnClickListener:

Also, we can declare the click event handler programmatically in the activity java file. This might be needed if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.

To declare the event handler programmatically, create a View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener).

For example:

Button button = (Button) findViewById(R.id.yourButton_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something
}
});

Android Button Example with Listener:

Here we simply add two numbers. We are going to create two text fields and one button to sum of two numbers. If the user clicks the button, the sum of two input values is displayed as the Toast message. Here we see both the declaring ways of a button and output will be like this:

Using the onClick method:

activity_main.xml

For onClick java file and the other way of declaring the button visit to this link by clicking it Woking with Android Button