Android ImageButton with image and text

I want to have a button with an image on top and some text at the bottom. Both the image and the text are determined at runtime, so I want to be able to combine ImageButton setImageBitmap and Button setText for each button.

Any ideas?

Thank you, Chris

+3
source share
4 answers

I finally found what I was looking for:

setCompoundDrawablesWithIntrinsicBounds allows me to assign bitmap / drawable to a button, while at the same time letting me use setText.

+2
source

... . :

 <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>
+3

For example: To set a bitmap on top of a button, do something like

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

+1
source

I do not think there is an easy way to do this.

I would create a custom-component .

0
source

Source: https://habr.com/ru/post/1754180/


All Articles