Search code examples
androidandroid-holo-everywhere

Holo theme and custom background for my button


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" />

Solution

  • 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.