Change appearance of button/imageview/imagebutton in android for different states

“States” here, I mean the press/focus states of control. We have normal state, on-focusing state and pressed-state. ( I used these, and don’t care about others ).
I find an interesting post at

Just re-post in my blog for later use.
Step 1 : Add images to resource.
button_normal_green.png – Default image button.
button_focused_orange.png – Display when button is focused, for example, when phone’s keypad is move (focus) on this button.
button_pressed_yellow.png – Display when button is pressed.
Step 2: Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.

Now, you can refer to this button via this Id : @drawable/new_button.

Step 3: Add Button
Open “res/layout/main.xml” file, add a normal button, and attach the background image to above “new_button” via “android:background=”@drawable/new_button”
File : res/layout/main.xml

Step 4: Code the main activity
A normal button with a simple click listener.

File :

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

Button imageButton;

public void onCreate(Bundle savedInstanceState) {



public void addListenerOnButton() {

imageButton = (Button) findViewById(;

imageButton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

"ImageButton (selector) is clicked!",





Reference :

3/ Sample source code can be found here


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: