1

I am trying to use drawableTop to show a picture above a text in a button.

My picture is from sdcard, not a drawable.

Also, the size of the button may change depending on how many buttons are show. More buttons, smaller buttons, then, smaller images.

Here is how I create my buttons:

btn = (Button) LayoutInflater.from(
                            getBaseContext()).inflate(
                            R.layout.buttonstyle, l1, false);

Here is the XML file buttonstyle:

<?xml version="1.0" encoding="UTF-8"?>
        <Button   xmlns:android="http://schemas.android.com/apk/res/android" 
            android:background="@drawable/roundcorners" 
            android:id="@+id/buttonTest" 
            android:scaleType="centerInside"
            android:cropToPadding="false"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="10dip"
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:text="Test text"
            android:textSize="40dip"
            android:textStyle="bold"
            android:textColor="#000000">
        </Button>

And here is how I create the drawableTop:

Drawable drawableTop = Drawable.createFromPath(endImagemVoltar); 
btn.setCompoundDrawables(null, drawableTop, null, null);

The text is shown in the button, but the image doesn't appear.

Where is the error?

Any help is appreciated!

Carlos Pereira
  • 1,914
  • 6
  • 24
  • 35

1 Answers1

1

I guess what's missing is to for your drawable to already have had setBounds(Rect) called also try loading the picture in a bitmap then getting it in a drawable:

Bitmap bitmapImage = BitmapFactory.decodeFile(endImagemVoltar);
Drawable bgrImage = new BitmapDrawable(bitmapImage);

To use SetBounds :

 public void setBounds (int left, int top, int right, int bottom)

for a 50x50 size for example use :

drawable.setBounds(0, 0, 50, 50);

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Hello Mona! Thank you for your reply, sorry... I am a noob, how can I setBounds? – Carlos Pereira Jul 02 '12 at 00:06
  • Hello Mona, thank you for you answer. Anyway, the size of images will change depeding on how many buttons are displayed. More buttons, smaller images. When I use ImageButton I don't need to worry about setting sizes. Is there a way to use your solution using something like "weight"? Thank you! – Carlos Pereira Jul 02 '12 at 21:50