Custom Buttons widget

Simple and plain buttons can do the job triggering some events in Android. But if you want to add more flavor and design to your UI, you can customize your buttons. Customization may include adding an image for your button, changing its image when pressed and/or displaying another image if it is focused.

In this section, we will create a simple customized button.

Here is an example that will show a customized button with three different image. The image will change depending on the state of the button.

Step 1.

Create a new project named HelloCustomButton. Below is the project structure of your new project.

Step 2.

Create/edit the layout so that it will display the customized button.

Step 3.

Create the customized button xml file as drawable.

Step 4.

Update the java source code so that it can handle the click event for the button.

Make sure to import the following:

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

Step 5.

Run the HelloCustomButton project and try clicking on the button to see the image change and the message displayed.

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*