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 –

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 –

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 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:

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:

<?xml version=”1.0" encoding=”utf-8"?>
<Button xmlns:android=”http://schemas.android.com/apk/res/android"

android:onClick=”yourMessage” />

In activity java file, handle the click event:

/** Called when the user click the button */
public void yourMessage(View view) {
// your code what you want to do

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:

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:

Using the onClick method:

<?xml version=”1.0" encoding=”utf-8"?>

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android" xmlns:app=”http://schemas.android.com/apk/res-auto" xmlns:tools=”http://schemas.android.com/tools" android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity4"> <EditText android:id=”@+id/editText1" android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_alignParentTop=”true” android:layout_centerHorizontal=”true” android:layout_marginTop=”61dp” android:ems=”10" android:inputType=”number” android:hint=”Enter First Number” tools:layout_editor_absoluteX=”84dp” tools:layout_editor_absoluteY=”53dp” />
EditText android:id=”@+id/editText2" android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_below=”@+id/editText1" android:layout_centerHorizontal=”true” android:layout_marginTop=”32dp” android:ems=”10" android:inputType=”number” android:hint=”Enter Second Number” tools:layout_editor_absoluteX=”84dp” tools:layout_editor_absoluteY=”127dp” /> <Button android:id=”@+id/button” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_below=”@+id/editText2" android:layout_centerHorizontal=”true” android:layout_marginTop=”109dp” android:text=”ADD” android:onClick=”sumOfTwoNumber” tools:layout_editor_absoluteX=”148dp” tools:layout_editor_absoluteY=”266dp” /> </RelativeLayout>

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

Hi everyone, myself Golap an Android app developer with UI/UX designer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store