Skip to content

Most visited

Recently visited

navigation

Button

public class Button
extends TextView

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.Button
Known Direct Subclasses
Known Indirect Subclasses


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 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

From class android.widget.TextView
From class android.view.View

Inherited constants

From class android.widget.TextView
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

Button(Context context)

Simple constructor to use when creating a button from code.

Button(Context context, AttributeSet attrs)

LayoutInflater calls this constructor when inflating a Button from XML.

Button(Context context, AttributeSet attrs, int defStyleAttr)

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

Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

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

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex)

Returns the pointer icon for the motion event, or null if it doesn't specify the icon.

Inherited methods

From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public constructors

Button

added in API level 1
Button (Context context)

Simple constructor to use when creating a button from code.

Parameters
context Context: The Context the Button is running in, through which it can access the current theme, resources, etc.

See also:

Button

added in API level 1
Button (Context context, 
                AttributeSet attrs)

LayoutInflater calls this constructor when inflating a Button from XML. The attributes defined by the current theme's android:buttonStyle override base view attributes. You typically do not call this constructor to create your own button instance in code. However, you must override this constructor when creating custom views.

Parameters
context Context: The Context the view is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML Button tag being used to inflate the view.

See also:

Button

added in API level 1
Button (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

This constructor allows a Button subclass to use its own class-specific base style from a theme attribute when inflating. The attributes defined by the current theme's defStyleAttr override base view attributes.

For Button's base view attributes see Button Attributes, TextView Attributes, View Attributes.

Parameters
context Context: The Context the Button is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML Button tag that is inflating the view.

defStyleAttr int: The resource identifier of an attribute in the current theme whose value is the the resource id of a style. The specified style’s attribute values serve as default values for the button. Set this parameter to 0 to avoid use of default values.

See also:

Button

added in API level 21
Button (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

This constructor allows a Button subclass to use its own class-specific base style from either a theme attribute or style resource when inflating. To see how the final value of a particular attribute is resolved based on your inputs to this constructor, see View(Context, AttributeSet, int, int).

Parameters
context Context: The Context the Button is running in, through which it can access the current theme, resources, etc.

attrs AttributeSet: The attributes of the XML Button tag that is inflating the view.

defStyleAttr int: The resource identifier of an attribute in the current theme whose value is the the resource id of a style. The specified style’s attribute values serve as default values for the button. Set this parameter to 0 to avoid use of default values.

defStyleRes int: The identifier of a style resource that supplies default values for the button, used only if defStyleAttr is 0 or cannot be found in the theme. Set this parameter to 0 to avoid use of default values.

See also:

Public methods

getAccessibilityClassName

added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

onResolvePointerIcon

added in API level 24
PointerIcon onResolvePointerIcon (MotionEvent event, 
                int pointerIndex)

Returns the pointer icon for the motion event, or null if it doesn't specify the icon. The default implementation does not care the location or event types, but some subclasses may use it (such as WebViews).

Parameters
event MotionEvent: The MotionEvent from a mouse

pointerIndex int: The index of the pointer for which to retrieve the PointerIcon. This will be between 0 and getPointerCount().

Returns
PointerIcon

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)