Android Button Introduction

| Page Views: 511

Android Button Tutorial and Examples.

A Button is a special type of TextView that has push capabilities like being pressed or clicked to perform an action.

Buttons have been available since API level 1.

Uses of a Button

Buttons are used in almost all type of applications with GUI components.

No. Use
1. Provide ability for users to initiate any type of action.

Programmatic Characteristics of a Button

  1. A button is a class like any other class:
    public class Button..{}
  2. A button is a widget:
    package android.widget;
  3. A button is a TextView.
    public class Button extends TextView {...}

Creating a Button

There are two general ways of creating a button in android.

  1. Imperatively via java code.
  2. Declaratively via XML code.

The Button class provides us 4 public constructors to programmatically create a button:

No. Constructor
1. Button(Context context)
2. Button(Context context, AttributeSet attrs)
3. Button(Context context, AttributeSet attrs, int defStyleAttr)
4. Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
######## Advantages of Imperative Way No. Advantage
1. Imperative creation of buttons allows us to use Java language so we have everything under one roof.
2. You can easily create buttons at runtime depending on various conditions.


Button myButton=new Button(MainActivity.this);

Buttons can also be created declaratively via XML. This is the most commonly used as it's very flexible.

######## Advantages of Declarative Way No. Advantage
1. Declarative creation of buttons allows us to use a declarative language XML which makes it quite easy
2. It's easily maintanable as the user interface is decoupled from your Java logic.
3. It's easier to share or download code and safely test them before runtime.
4. You can use XML generated tools to generate XML

Here's an example:



Here's a simple button example that when clicked shows a Toast message.

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        final Button showToastBtn=findViewById(;
        showToastBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"IC1011 is the largest Galaxy ever discovered",Toast.LENGTH_SHORT).show();

And here's the button xml code that you can add in a layout:

        android:text="Show Toast"

Children and Grandchildren of Button

Buttons have direct and indirect subclasses:

Button Children

Here are the direct subclasses:

No. Class Description
1. AppCompatButton A Button with compatible features on older versions of Android.
2. CompoundButton A CompoundButton is a button with two states: checked and unchecked.
Button Grandchildren

Here are the indirect subclasses of the Button class:

No. Class Description
1. CheckBox A type of CompoundButton that can be checked and unchecked.
2. AppCompatCheckBox A CheckBox supporting compatible features on older version of Android.
3. RadioButton A type of CompoundButton that can be either be checked or unchecked. But unlike a CheckBox once a radiobutton is checked user cannot uncheck it.
4. ToggleButton A type of CompoundButton that renders the checked or unchecked states with a "light" indicator that can be toggled "ON" or "OFF".
5. Switch A type of CompoundButton allows us swicth between two options.
6. SwitchCompat A Switch button that supports devices as old as API v7.

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post