Buttons in Android – OverView and Implementation

Buttons in Android – Implementation and Advanced Tips and Tricks

Android Buttons is an important View which is created using the Button Class. The XML implementation for Button is given below.

Buttons in Android Implementation
Button XML implementation

The Button XML tag needs two mandatory properties, they are

android:layout_width & android:layout_height

There are various types of Buttons like Radio ,Toggle, Compound Buttons. We will see about it in the future posts.

 

Creating a Image Button

It is pretty simple to create a Image button.The XML tag ImageButton is responsible for creating it. It has the same properties as that of a Button.

ImageButton XML Implementation
ImageButton in android

The android:src property is set to include the icon to be present in the Button.

You can also include text into the Button and Image at the same time using the property.

android:drawableLeft (Load the icon/image) and android:src (With the text value for the button).

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Value"
    android:drawableLeft="@drawable/bag"/>

 

Using Listeners for Buttons

Button listeners are very useful to capture the button press or any activities on the button (Double press is also available as a listner). It is to be noted that, the button click action can also be specified in the XML itself, but it is not recommended. Implementation for onClickListener is like below.

signupback.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

            //Do some action here

    }
});

Note that, onClickListener has a function called onClick which has to be overridden to create your action.

The onClick function is fired every time the button is clicked and the respective action is performed.

Another way to implement the onClick is like below. (Calling the method in XML document).

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Value"
    android:onClick="methodname"
    android:drawableLeft="@drawable/bag"/>

This method is however not advised because of the lack of control in the Activity. XML calls are difficult to track in case of a complex project.

 

Buttons – Playing around and Creating that perfect One

If you have read my post here, there is a need to make sure your UI is appealing to the end User. It is mandatory to create the properly designed button. I will discuss about the importance of some of the properties and how to use them to create a good looking button for your project.

Some of the important button properties that we will be needing to create the button is
android:gravity="center"//Gravity is important to align the text inside the button
android:textColor="#fff" //Text Color is important inorder to improve the visual appeal
Additionally

android:padding

android:margin

android:background

android:height

android:width

If you want to know how to put all of this together, check out my tutorial here. You can create a Android Application in less than 3 hours!!.

The above properties are important inorder to improve the visual appeal of the buttons you create.

Check this Material Design Google document for more information.

“Learn and Be Curious”

Leave a Comment