n4h1n
n4h1n

Reputation: 357

set background color and set image over background in ImageButton - Android

I need to set a background color and an image over that background, how can i do it? Is necesary the image to have transparency? I need to control states too: pressed, focused.

Any help will be grateful

Upvotes: 1

Views: 9699

Answers (2)

Evan Bashir
Evan Bashir

Reputation: 5551

Create an ImageView (Use a transparent .PNG for your src image):

res/layout/yourlayout.xml

<ImageView
    android:layout_width=:"wrap_content"
    android:layout_height=:"wrap_content"
    android:background="@drawable/colorstates"
    android:src="@drawable/image" />

res/drawable/colorstates.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/regular"
        android:state_checked="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/pressed"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/pressed"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/regular" />
</selector>

Upvotes: 6

SimonSays
SimonSays

Reputation: 10977

yes the image needs a transparent background, otherwise it will cover the background color. Use the background attribute for the color and src for the image. Also check out the doc. For the pressed state, use google!

Upvotes: 0

Related Questions