Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change button background on touch

Tags:

java

android

i want to change the green background of a button when this is touched without using OnTouchListener. i have this situation: i put some butons in a layer, then the layer in another layer, and the last layer in onother layer. when i touch the button the background is changing(i m using OnTouchListener right now) but if i drag the finger outside of the button and then get it out of the screen the background of the button remains the image from the state when it s touch(otherwise if i click the button and the the finnger off the button it is k the background is changing)

like image 502
moctavianro Avatar asked Nov 15 '11 06:11

moctavianro


1 Answers

1. Prepare 3 images for button states, and put it into resource/drawable folder.

2. create a new XML file in res/drawable/ folder, in whatever name you want, in this case, we just give a name as my_button.xml. This file defined which button state is belong to which image.

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

File : res/drawable/my_button.xml

create xml file using the button image like this with my_button.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/button_pressed_yellow"
      android:state_pressed="true" />
  <item android:drawable="@drawable/button_focused_orange"
      android:state_focused="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>

add a normal button, and attach the background image to above “my_button” via

android:background:@drawable/my_button
like image 98
RajaReddy PolamReddy Avatar answered Sep 25 '22 23:09

RajaReddy PolamReddy