0

I have some problems to add the blue color over the button when the user press it. It works if there is no drawable in background for this button but in my case, i have to add a custom background and i want the blue color when the user clicks on the button. Here is my code

<Button
    android:id="@+id/create_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/info_account"
    android:layout_centerHorizontal="true"
    android:background="@drawable/btn_create_profile" />
mrroboaat
  • 5,602
  • 7
  • 37
  • 65

2 Answers2

1

Blue color is not something that platform draws for you. Standard buttons have a selector drawable as their background, which involves a set of images for a view. So for button for example it is a standard button image, pressed button image (with blue overlay drawn above), disabled (half transparent), etc. Button knows it's current state and displays appropriate image.

So what you want to do is to draw a pressed button yourself and create a selector drawable like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item  android:state_pressed="true" android:drawable="@drawable/your_pressed_button/>
    <item  android:drawable="@drawable/your_normal_button/>

</selector>

I believe it's worth reading about Drawable Resources. You can also find examples of button states generated here.

EvilDuck
  • 4,386
  • 23
  • 32
  • So i have to create different drawables for differents states ? There is no simple way to inform the user that he is clicking the button ? – mrroboaat May 15 '13 at 09:01
  • No. The only thing I can see is inheriting the Button component and drawing blue overlay on top of it yourself on canvas. But I personally would avoid that. – EvilDuck May 15 '13 at 09:19
  • Okay ! I know the selector but all my buttons have a custom drawable so for every button I have to create xml file. – mrroboaat May 15 '13 at 09:25
0

You should make custom drawable :

For this you have to simply create a xml file in your drawable folder and write :

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

  <item  android:state_pressed="false"
       android:drawable="@drawable/ic_back" />

  <item android:state_pressed="true"
      android:drawable="@drawable/ic_back_pressed" />

  <item android:state_focused="true"
      android:drawable="@drawable/ic_back_pressed" />

</selector>

and now set this drawable in background of your button.

Here, in normal state background id ic_back and pressed and focus state background is ic_back_pressed

For creating solid drawable shapes (for example, if you want solid color backround as drawable you can go here.. )

Chirag Jain
  • 1,612
  • 13
  • 20