Added in API level 1

Button


open class Button : TextView
kotlin.Any
   ↳ android.view.View
   ↳ android.widget.TextView
   ↳ android.widget.Button

A user interface element the user can tap or click to perform an action.

To display a button in an activity, add a button to the activity's layout XML file:

<Button
      android:id="@+id/button_id"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="@string/self_destruct" />

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

public class MyActivity extends Activity {
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
 
          setContentView(R.layout.content_layout_id);
 
          final Button button = findViewById(R.id.button_id);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  // Code here executes on main thread after user presses button
              }
          });
      }
  }

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

The system executes the code in onClick on the main thread. This means your onClick code must execute quickly to avoid delaying your app's response to further user actions. See Keeping Your App Responsive for more details.

Every button is styled using the system's default button background, which is often different from one version of the platform to another. If you are not satisfied with the default button style, you can customize it. For more details and code samples, see the Styling Your Button guide.

For all XML style attributes available on Button see Button Attributes, TextView Attributes, View Attributes. See the Styles and Themes guide to learn how to implement and organize overrides to style-related attributes.

Summary

Inherited XML attributes
Inherited constants
Public constructors
Button(context: Context!)

Simple constructor to use when creating a button from code.

Button(context: Context!, attrs: AttributeSet!)

LayoutInflater calls this constructor when inflating a Button from XML.

Button(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

This constructor allows a Button subclass to use its own class-specific base style from a theme attribute when inflating.

Button(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

This constructor allows a Button subclass to use its own class-specific base style from either a theme attribute or style resource when inflating.

Public methods
open CharSequence!

open PointerIcon!
onResolvePointerIcon(event: MotionEvent!, pointerIndex: Int)

Inherited functions