3

I want to have a button that has an image on top and some text on bottom. Both the image and text are decided during runtime, so I want to be able to combine ImageButton's setImageBitmap and Button's setText for each button.

Any ideas?

Thanks Chris

Chris
  • 3,787
  • 13
  • 42
  • 49
  • Duplicate: http://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton – ognian Jul 12 '10 at 06:23
  • I looked at that question before asking, but I cant go with the answer specified there, because, 1. I cannot set the image as a background, I want the image on top and text on bottom, not text over image. 2. I need to set things programatically, cannot use xml – Chris Jul 12 '10 at 06:32
  • Then check out my answer in another duplicate: http://stackoverflow.com/questions/3148713/inflating-a-view-into-button/3149052#3149052 – ognian Jul 12 '10 at 07:13

4 Answers4

3

Surround both image Button and text View code in .... to override text on image button's background. i.e:

 <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="69dp"
            android:layout_marginTop="96dp"
            android:background="@drawable/icon"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Droid"
            android:textColor="#ff0000"
            android:textSize="24dip" >
        </TextView>
    </FrameLayout>
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
Nishant
  • 93
  • 4
2

I finally found what I was looking for:

setCompoundDrawablesWithIntrinsicBounds lets me assign a bitmap/drawable for a Button, at the same time letting me use setText.

Chris
  • 3,787
  • 13
  • 42
  • 49
  • That is for Button with text and image, not ImageButton, so that's not yet a solution of the original question but a workaround. – A-Live Dec 12 '12 at 09:20
1

For eg: To set a bitmap image on top of the button, do something like

button.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(bitmapimage), null, null);

Chris
  • 3,787
  • 13
  • 42
  • 49
0

I don't think there is an easy way to do that.

I would create a custom-component.

Macarse
  • 91,829
  • 44
  • 175
  • 230