-1

Possible Duplicate:
How to Change color of Button in Android when Clicked?

I want to change the color of button when it clicked... How i can do it? I don't want to do it by using drawable folder...

Community
  • 1
  • 1
Pratibha
  • 257
  • 1
  • 3
  • 10

1 Answers1

2

use a selector , create a file xml in your drawable/ folder and name it bg_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/back_button_clicked" android:state_pressed="true"></item>
    <item android:drawable="@drawable/back_button_clicked" android:state_focused="true"></item>
    <item android:drawable="@drawable/back_button_normal"></item>

</selector>

and then in your xml layout , define your button like this :

<Button android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_button" />

NB : back_button_clicked and back_button_normal are drawables for the background of your button. the drawable back_button_clicked will be the background of your button when it will be clicked , and back_button_normal will be the background of your button in normal case.

EDIT : here is a tutorial for more explanation . and here is another one

Houcine
  • 24,001
  • 13
  • 56
  • 83
  • I want to do it in my OnClickListener function.. Don't want use drawable ... :( – Pratibha Nov 27 '12 at 16:18
  • there is no need to do it with onClickListener , that's why selectors are here :) – Houcine Nov 27 '12 at 16:19
  • Okkkiieee .. i will try this now ..:) – Pratibha Nov 27 '12 at 16:20
  • see my edits NB for explanation :) – Houcine Nov 27 '12 at 16:21
  • This Button in xml file which in layout folder... – Pratibha Nov 27 '12 at 17:26
  • in my java file ... final Button on_but = (Button) findViewById(R.id.on_basket_but); on_but.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { on_but.setBackgroundResource(R.drawable.changecolor); } }); – Pratibha Nov 27 '12 at 17:27
  • and I create drawable folder in res , in that changecolor.xml file i am not gettibg that what i can write in in this tag..?? please tell me... :( – Pratibha Nov 27 '12 at 17:28
  • you don't need to set the `onclickListener` and change the backgroundResource of your button programmatically , just add the selector ( `change_color.xml`) on your `drawable/` folder, and specify the `android:background="@drawable/change_color"` on your `Button `tag – Houcine Nov 27 '12 at 17:31
  • see the toturial in my edits for more explanation. – Houcine Nov 27 '12 at 17:33
  • yeah i tried your answer. but m not getting that what to write in drawable xml file ? – Pratibha Nov 27 '12 at 17:36
  • you have two images( png) that you want them to be the background of your button , put them in your drawable folder . see the tutorial that i've added to my answer and you will understand :) – Houcine Nov 27 '12 at 17:37
  • you're welcome :) , accept the answer so others can get informations and help from it ^-* – Houcine Nov 27 '12 at 18:02