Working with Android Buttons
A Button is a Push-button that can be pressed by the user to act. The android.widget.button is a subclass of TextView class and CompoundButton is the subclass of the Button class. The button consists of text or an icon (or both text and an icon) that describes what action occurs when the user clicks it.
Buttons Attributes:
Inherited from android.widget.TextView Class –
- android:autoText
If set specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
2. android:drawableBottom
This is drawable to be drawn below the text.
3. android:drawableRight
This is drawable to be drawn to the right of the text.
4. android: editable
If set specifies that this TextView has an input method.
5. android: text
This is the Text to display.
Inherited from android.view.View Class –
- android:background
This is drawable to use as the background.
2. android:contentDescription
This defines the text that briefly describes the content of the view.
3. android: id
This supplies an identifier name for this view.
4. android:onClick
This is the name of the method in this View’s context to invoke when the view is clicked.
5. android: visibility
This controls the initial visibility of the view.
We can create a button in the layout file in three ways using text, an icon, or both text and an icon:
- 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”
android:contentDescription=”@string/button_icon_desc”
… />
- 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”
… />
Define Click Events in Button:
1.Using onClick:
When the user clicks a button, the Button object receives an on-click event.
To define it for a button, add the android:onClick attribute to the <Button> element in the XML layout file. The value for the attribute is given by you whatever you want to call from that attribute. The Activity implements the corresponding method.
For example, here’s a layout with a button using android:onClick:
<?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=”yourMessage” />
In activity java file, handle the click event:
/** Called when the user click the button */
public void yourMessage(View view) {
// your code what you want to do
}
The method declares in the android:onClick attribute must have that format exactly as shown above. The method must:
- Be public
- Return as void
- Define a View as its only parameter (this will be the View that was clicked)
2.Using OnClickListener:
Also, we can declare the click event handler programmatically in the activity java file. This might be needed if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
To declare the event handler programmatically, create a View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener).
For example:
Button button = (Button) findViewById(R.id.yourButton_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something
}
});
Android Button Example with Listener:
Here we simply add two numbers. We are going to create two text fields and one button to sum of two numbers. If the user clicks the button, the sum of two input values is displayed as the Toast message. Here we see both the declaring ways of a button and output will be like this:
Using the onClick method:
activity_main.xml
<?xml version=”1.0" encoding=”utf-8"?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android" xmlns:app=”http://schemas.android.com/apk/res-auto" xmlns:tools=”http://schemas.android.com/tools" android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity4"> <EditText android:id=”@+id/editText1" android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_alignParentTop=”true” android:layout_centerHorizontal=”true” android:layout_marginTop=”61dp” android:ems=”10" android:inputType=”number” android:hint=”Enter First Number” tools:layout_editor_absoluteX=”84dp” tools:layout_editor_absoluteY=”53dp” />
<EditText android:id=”@+id/editText2" android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_below=”@+id/editText1" android:layout_centerHorizontal=”true” android:layout_marginTop=”32dp” android:ems=”10" android:inputType=”number” android:hint=”Enter Second Number” tools:layout_editor_absoluteX=”84dp” tools:layout_editor_absoluteY=”127dp” /> <Button android:id=”@+id/button” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_below=”@+id/editText2" android:layout_centerHorizontal=”true” android:layout_marginTop=”109dp” android:text=”ADD” android:onClick=”sumOfTwoNumber” tools:layout_editor_absoluteX=”148dp” tools:layout_editor_absoluteY=”266dp” /> </RelativeLayout>
For onClick java file and the other way of declaring the button visit to this link by clicking it Woking with Android Button