How to add button caption on image button

I am going to use the image button in my application. I need to display the button title on the button in which I use this code, but did not display the signature

<ImageButton android:id="@+id/try_button" android:background="@drawable/custom_button" android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="10dp" android:text="@string/trybutton_caption" /> 

How can i do this?

Thanks.

+4
source share
4 answers

Use Button instead of ImageButton .

Used here:

 android:drawableTop="@drawable/image" android:text="@string/text" 

FYI, using drawableTop , the drawableTop will be drawn over the text.

+1
source

Finally, I did this with your help:

 <Button android:id="@+id/try_button" android:background="@drawable/custom_button" android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="10dp" android:text="@string/trybutton_caption"/> 
+2
source

You need to create a layout containing an image view and a text view. Then you use the onClick event of the parent layout to treat it like a button. Here is an example:

 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/image_button_background" android:gravity="center" android:onClick="doCustomers" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:src="@drawable/ic_menu_box" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="10dp" android:text="Customers" /> </LinearLayout> 
0
source

Another option would be to create a TextView and set the composite vertex up for your image:

 android:drawableTop="@drawable/custom_button" 

or through the code:

 textView.setCompoundDrawableWithIntrinsicBounds(0, R.drawable.custom_button, 0, 0) 
0
source

All Articles