Chris
Chris

Reputation: 3807

Android ImageButton with Image and Text

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

Upvotes: 3

Views: 14573

Answers (4)

Nishant
Nishant

Reputation: 93

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>

Upvotes: 3

Chris
Chris

Reputation: 3807

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

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

Upvotes: 1

Chris
Chris

Reputation: 3807

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.

Upvotes: 2

Macarse
Macarse

Reputation: 93133

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

I would create a custom-component.

Upvotes: 0

Related Questions