Android Button

In the Android operating system, buttons are a user interface element used to trigger actions or navigate between screens in an app. Buttons can be created using the Button class, which is part of the Android SDK.

There are different types of buttons in android such as Radio Button, Toggle Button, Compound Button etc. The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.

Buttons can be customized in a variety of ways, including setting the text that appears on the button, changing the button's background color or image, and specifying a listener that will be called when the button is clicked. Here is an example of how to create a button in Android:

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

In this example, a Button is created with an ID of "my_button", a width and height set to "wrap_content", and text set to "Click me". The onClick attribute is set to "onButtonClick", which means that when the button is clicked, the onButtonClick() method will be called.

Here is an example of how to handle button clicks in Android:

public class MainActivity extends AppCompatActivity {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                
                Button myButton = findViewById(R.id.my_button);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Do something when the button is clicked
                    }
                });
            }
        }

In this example, a Button with an ID of "my_button" is retrieved using findViewById(), and a click listener is set using setOnClickListener(). When the button is clicked, the onClick() method of the listener is called, allowing you to perform some action in response to the button click.

Attributes of Button in Android

Now let's we discuss some important attributes that helps us to configure a Button in our xml file (layout).

Attribute Description
android:id It is used to uniquely identify the control
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:text It is used to set the text.
android:textColor It is used to change the color of text.
android:textSize It is used to specify the size of the text.
android:textStyle It is used to change the style (bold, italic, bolditalic) of text.
android:background It is used to set the background color for button control.
android:padding It is used to set the padding from left, right, top and bottom.
android:drawableBottom It's drawable to be drawn to the below of text.
android:drawableRight It is drawable to be drawn to the right of text.
android:drawableLeft It is drawable to be drawn to the left of the text.


Overall, buttons are a fundamental user interface element in Android that allow users to interact with an app and trigger actions. With the Button class and its customization options, developers can create buttons that match their app's design and functionality needs.

Next Article ❯