# Button

&#x20;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.

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-L_QrBYawGA0WuESxcg6%2F-L_QrctEJdIqwHG31JMD%2Fdg_button_types_composite.png?alt=media\&token=53226eb1-5227-4429-b563-66e9212d1278)

## Raised button

&#x20;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()`](https://developer.android.com/reference/android/view/View.html#findViewById\(int\)) 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
         }
     }
    ```

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-L_QrBYawGA0WuESxcg6%2F-L_QufRNSrCD-cKB8cUx%2Fdg_button_listener_design_pattern.png?alt=media\&token=990008c6-4288-4671-a97e-2c6dbb6cd66f)
