Class Button

All Implemented Interfaces:
Drawable.Callback, ViewTreeObserver.OnPreDrawListener
Direct Known Subclasses:
CompoundButton, Switch

public class Button extends TextView
A user interface element the user can tap or click to perform an action.

To specify an action when the button is pressed, set a click listener on the button object in the corresponding fragment code:


 public class MyFragment extends Fragment {
     @Nullable
     @Override
     public View onCreateView(@Nullable ViewGroup container, ...) {
         final Button button = new Button;
         button.setOnClickListener(v -> {
              // Code here executes on UI thread after user presses button
         });
         // ...
         return button;
     }
 }
 

The above snippet creates an instance of View.OnClickListener and wires the listener to the button using View.setOnClickListener(icyllis.modernui.view.View.OnClickListener). As a result, the system executes the code you write in lambda expression after the user presses the button.

The system executes the code in onClick on the UI thread. This means your onClick code must execute quickly to avoid delaying your app's response to further user actions.