Android Guides | Samples

Android.Widget.Button Class

Represents a push-button widget.

See Also: Button Members

Syntax

[Android.Runtime.Register("android/widget/Button", DoNotGenerateAcw=true)]
public class Button : TextView

Remarks

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

java Example

 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
             }
         });
     }
 }

xml Example

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

java Example

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

Button style

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

However, instead of applying an NoType:android/view/View$OnClickListener;Href=../../../reference/android/view/View.OnClickListener.html to the button in your activity, you can assign a method to your button in the XML layout, using the NoType:android/R$attr;Href=../../../reference/android/R.attr.html#onClick attribute. For example:

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:

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

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 NoType:android/R$attr;Href=../../../reference/android/R.attr.html#background attribute. For more information and an example, see State List Drawable.

See the Buttons guide.

XML attributes

See NoType:android/R$styleable;Href=../../../reference/android/R.styleable.html#Button, NoType:android/R$styleable;Href=../../../reference/android/R.styleable.html#TextView, NoType:android/R$styleable;Href=../../../reference/android/R.styleable.html#View

[Android Documentation]

Requirements

Namespace: Android.Widget
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Android.Widget.Button are listed below.

See Also: Inherited members from Android.Widget.TextView

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.