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


Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.

A typical use of a push-button in an activity would be the following:

 public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
 }

However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

 public void selfDestruct(View view) {
     // Kabloey
 }

The View passed into the method is a reference to the widget that was clicked.

Button style

Every Button is styled using the system's default button background, which is often different from one device to another and from one version of the platform to another. If you're not satisfied with the default button style and want to customize it to match the design of your application, then you can replace the button's background image with a state list drawable. A state list drawable is a drawable resource defined in XML that changes its image based on the current state of the button. Once you've defined a state list drawable in XML, you can apply it to your Button with the android:background attribute. For more information and an example, see State List Drawable.

See the Buttons guide.

XML attributes

See Button Attributes, TextView Attributes, View Attributes

Summary

Inherited XML attributes

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

Inherited constants

From class android.view.View

Inherited fields

From class android.view.View

Public constructors

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

Public methods

CharSequence getAccessibilityClassName()

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

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)

Parameters
context Context

Button

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

Parameters
context Context
attrs AttributeSet

Button

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

Parameters
context Context
attrs AttributeSet
defStyleAttr int

Button

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

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

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
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

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.