Button In Java Code - Android Snippets

A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it. Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:

With text, using the Button class:


 <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_text"
            ... />

With an icon, using the ImageButton class:

<ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/button_icon"
            ... />

With text and an icon, using the Button class with the android:drawableLeft attribute:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:drawableLeft="@drawable/button_icon"
        ... />

Responding to ClickEvents


When the user clicks a button, the Button object receives an on-click event.For example, here's a layout with a button using android:onClick: 

XML Snippet


<?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/button_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
            android:onClick="sendMessage"/>

Within the Activity that hosts this layout, the following method handles the click event: 

Java Snippet


/** Called when the user touches the button */
public void sendMessage(View view)
{
 // Do something in response to button click
}

Using an OnClickListener

You can also declare the click event handler programmatically rather than in an XML layout.

For Example 

Button button=(Button)findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// Do something in response to button click
}
});

Styling Your Button

The appearance of your button (background image and font) may vary from one device to another, because devices by different manufacturers often have different default styles for input controls.

To customize individual buttons with a different background, specify the android:background attribute with a drawable or color resource.

Alternatively, you can apply a style for the button, which works in a manner similar to HTML styles to define multiple style properties such as the background, font, size, and others. For more information about applying styles, see Styles and Themes.

Borderless Button

One design that can be useful is a "borderless" button. Borderless buttons resemble basic buttons except that they have no borders or background but still change appearance during different states, such as when clicked. To create a borderless button, apply the borderlessButtonStyle style to the button.

For Example
<Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"
        style="?android:attr/borderlessButtonStyle" />
You can read more valuable information in brief as ANDROID SNIPPETS.
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment