0

I would like to define a button in Android, through XML, which, below the standard Button graphics (change color when clicked, slightly rounded edges,...), shows an image of my choice. I would like the final product to be somewhat like this:

enter image description here

I have tried change the src and background of an ImageButton, but it does not provide the intended effect. Could you please point me some way of achieving this?

Paulo Avelar
  • 2,140
  • 1
  • 17
  • 31
AmiguelS
  • 805
  • 2
  • 10
  • 28

2 Answers2

0

Maybe that's not exactly what you mean by standard, but when you set a background, you end up having to recreate the behavior when the button is clicked. The best way to do it in Android is by using a selector as your button's background. Create a XML drawable with the selector in it.

Example:

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

That way, when your button is clicked, it will change the background to other image that does what you want.

EDIT:

  1. There are other modifiers such as focused, if you need.
  2. If you want Lollipop's ripple effects, I think you can create a Layout and put your button inside it. Change the Layout background and set the button background to transparent. If that doesn't work, try adding android:background="?android:attr/selectableItemBackground" to your button.
Paulo Avelar
  • 2,140
  • 1
  • 17
  • 31
0

As Paulo said, you can achieve the click effect with a selector. See this answer (https://stackoverflow.com/a/30192562) it gives the code for a very nice and customizable ripple effect.

Community
  • 1
  • 1
DHM47
  • 66
  • 5