Change appearance of button when pressed

Put your problem here if it does not fit any of the other categories.

Change appearance of button when pressed

Postby OHSWE » Mon Feb 28, 2011 5:14 pm


I am new to Android and i am trying to code an app that does the following:

In my app I have three imagebuttons. And when I press the button the image should change. And when I release the button the image should change back again. So as long as I press the button the appearance of the button should change. And the same thing should happend when I press a key, for example as long as I press the "1"-key the first of my three button should change appearance (the buttons image should change).

I have search the net for examples but i havent found any. So if anyone here could help me I would be happy!

Posts: 2
Joined: Mon Feb 28, 2011 3:22 pm


Re: Change appearance of button when pressed

Postby strider2023 » Tue Mar 01, 2011 4:30 pm

Well here is how you would do it...

Step 1: Make three different buttons.
Step 2: Make an xml file in your drawable folder and name it something like "button_config.xml" and copy paste the following piece of code. Please do remember to change the name of the button images you are configuring for...(here the button images names are..delete_idle and delete pressed).

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/delete_idle" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/delete_pressed" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/delete_pressed" />
    <item android:drawable="@drawable/delete_idle" />

Step 3: Then where you are defining your button (i mean the layout) write the following piece of code.

Code: Select all

Hope that helps...feel free to ask me if you have any questions.
Senior Developer
Senior Developer
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Change appearance of button when pressed

Postby jonbonazza » Wed Mar 09, 2011 10:39 pm

The above is the best method for doing this, however you should NEVER use exact pixels in anything android related. instead, use dp.
Master Developer
Master Developer
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 3 guests