androiddrawablecompound-drawables

DrawableTop not showing drawable


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!


Solution

  • 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);