views:

336

answers:

2

Title says it all: I need button that has a replicated background pattern and normal button text on top - how to specify this in layout XML?

A: 

Consider using ImageButton instead of using regular one.

Sorantis
`ImageButton` doesn't allow you to set a text label.
Christopher
Will ImageButton replicate the background image?
Eno
@Chris: Thought so...
Eno
A: 

Override android:background on the Button in question. Or, for reuse you could declare your own style, overriding android:background from the base button style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyButtonStyle" parent="Widget.Button">
    <item name="android:background">@drawable/fancy_button</item>
  </style>
</resources>

In either case, your fancy_button drawable would be an image, or more likely a StateListDrawable.

For the Button instances you wish to apply this to, you'd do:
<Button style="@style/MyButtonStyle" />.

Christopher
@Chris: I hadn't looked at the styling/theming in Android and that is a good approach to me.
Eno
@Chris: Im assuming StateListDrawable allows me to swap images based on state? How would I use that for my buttons?
Eno
See the example here: http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view/2016344#2016344You just declare which image you want to show when the button is focused/pressed/disabled/none-of-the-above etc.
Christopher