1

i'm using Buttons (or ImageButtons) to show the icons of my app. I use this to set the state of the buttons, with a image for the button pressed and another image for the button unpressed:

selector(this, favorites,  R.drawable.icon_star_mark,  R.drawable.icon_star_mark_selected);

public static void selector(Context c, Button b, int normal_image, int pressed_image){
        StateListDrawable states = new StateListDrawable();
        states.addState(new int[] {android.R.attr.state_pressed}, c.getResources().getDrawable(pressed_image));         
        states.addState(new int[] { }, c.getResources().getDrawable(normal_image));      
        b.setBackgroundDrawable(states);
    }

I need to put text below the icon, but i want to learn doing it without using a LinearLayout with a icon and a textview below it. I want to learn doing that if it is possible to achiev it.

This is the way i'm creating the icon:

Button map= new Button(this);
selector(this, map,  R.drawable.icon_map,  R.drawable.icon_map_selected);
map.setText("Map");

The problem is that the text is being displayed above the icon, and not below the icon.

It is possible to achieve that with java code? (placing the text of the icon below the icon without using a linear layout with a textview below the icon)

Thanks in advance

Pableras84
  • 1,195
  • 5
  • 18
  • 30

1 Answers1

6

It's possible using drawableTop attribute. The selector goes into android:background and the icon into android:drawableTop attr.

<Button
        android:id="@+id/btn"
        style="@style/DashboardButton"
        android:drawableTop="@drawable/any_drawable"
        android:text="@string/any_text" />

Or you can set drawables programatically using button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
João Melo
  • 508
  • 5
  • 20
  • I'm trying to learn doing this with Java code, but i can't find the way to set drawableTop with java code, Did you know it? – Pableras84 Oct 29 '12 at 20:32
  • wich values i must give to (left, top, right, bottom) to set the text below the image? – Pableras84 Oct 29 '12 at 20:36
  • 1
    If you want a drawable on top, just supply it on the top parameter. The others you can set to null. – João Melo Oct 29 '12 at 20:37
  • 1
    @Pableras84 `map.setCompoundDrawablesWithIntrinsicBounds(0, R.id.your_icon, 0, 0);` – iTurki Oct 29 '12 at 20:38
  • nice, but i'm using my selector function to giving two states to the bitmap, pressed and unpressed with different drawables... it can be achieved with this? – Pableras84 Oct 29 '12 at 21:24
  • @Pableras84 Then you should pass the selector id instead of the icon. It should work. – iTurki Oct 29 '12 at 21:35
  • my selector doesn't have a id, it is generated with java code. I think i didn't understand youu, please, can you give me a code example? – Pableras84 Oct 29 '12 at 21:50
  • @Pableras84 You can use another method: [`setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)`](https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28android.graphics.drawable.Drawable,%20android.graphics.drawable.Drawable,%20android.graphics.drawable.Drawable,%20android.graphics.drawable.Drawable%29). E.g. `map.setCompoundDrawablesWithIntrinsicBounds(0, states, 0, 0);` – Sergey Glotov Oct 30 '12 at 05:51