4

I am going to use image button in my app And I need to display button caption on the button I'm using this code but it did not show caption

<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?

Thank you.

Raymond Lukanta
  • 495
  • 3
  • 14
Tikitaka
  • 460
  • 2
  • 7
  • 15

4 Answers4

2

Use Button instead of ImageButton.

There use:

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

FYI by using drawableTop, the drawable will be drawn above the text.

Raymond Lukanta
  • 495
  • 3
  • 14
stinepike
  • 54,068
  • 14
  • 92
  • 112
2

Finally I did that 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"/>
Termininja
  • 6,620
  • 12
  • 48
  • 49
Tikitaka
  • 460
  • 2
  • 7
  • 15
0

You need to create a layout that contains an image view and a text view. You then 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>
Eric Parshall
  • 731
  • 9
  • 15
0

Another option might be to create a TextView and set the compound drawable top to your image:

android:drawableTop="@drawable/custom_button"

or through code:

textView.setCompoundDrawableWithIntrinsicBounds(0, R.drawable.custom_button, 0, 0)
britzl
  • 10,132
  • 7
  • 41
  • 38