Button

Android offers several types of Button elements, including raised buttons and flat buttons as shown in the figure below. Each button has three states: normal, disabled, and pressed.

Raised button

Use the Button element in the layout file. A raised Button is the default style.

 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="this is button text" 
     <!-- more attributes ... --> 
 />

Flat button

<Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="this is button text" 
     android:background="@color/colorAccent"
    android:padding="5dp"
     <!-- more attributes ... --> 
 />

Flat button borderless

<Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="this is button text" 
     style="@style/Widget.AppCompat.Button.Borderless"
     <!-- more attributes ... --> 
 />

Implementing click listener

  1. Use the findViewById() method of the View class to find the Button in the XML layout file:

     Button button = findViewById(R.id.button_send);
  2. Override the onClick() method:

     button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             // Do something in response to button click
         }
     }

Last updated

Was this helpful?