7

I have a ImageButton which looks like this.

 <ImageButton
            android:id="@+id/ImageButton"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:layout_marginBottom="6px"
            android:layout_marginRight="3px"
            android:layout_toRightOf="@+id/Logo"
            android:background="@drawable/button_bg_purple"
            android:scaleType="fitStart"
            android:src="@drawable/ImageButton" />


 ImageButton ImgButton= (ImageButton) this.findViewById(R.id.ImageButton);

Now I need to add a dynamic text in ImageButton programmatically and I cannot use button. How can I do it?

Thanks In Advance ...

Suniel
  • 1,449
  • 8
  • 27
  • 39
  • 1
    Why can't you use a Button? ImageButtons don't have a text property. They extend ImageView. If you don't want to use a Button, then use a TextView. OR... you'll have to draw your text inside the ImageButton PROGRAMMATICALLY... such an overkill! – Phantômaxx Feb 14 '14 at 10:27
  • ImageButton can not have text! – E A Apr 22 '15 at 10:48

5 Answers5

15

ImageButton cannot contain text:

Options you can try

1) use Button instead of ImageButton

2) use ImageButton and TextView below to show text

Hope this could help

Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46
5

You cannot set text to ImageButton because it has no method as setText() or android:text property.

ImageButtons can't have text (or, at least, android:text isn't listed in its attributes). It looks like you need to use Button (and look at drawableTop or setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)).

Instead of ImageButton try with Button. You can able to add the button background image.So try with Button instead of ImageButton.

Community
  • 1
  • 1
Jebasuthan
  • 5,538
  • 6
  • 35
  • 55
3

you use of drawableRight or drawableLeft attribute and use text attribute for create Button with text and image.

<Button 
 android:id="@+id/buttonok" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"
 android:drawableLeft="@drawable/buttonok"
 android:text="OK"/>
Digicom
  • 154
  • 1
  • 2
  • 12
0

Try this:

<Button
    android:id="@+id/Button"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginBottom="6px"
    android:layout_marginRight="3px"
    android:layout_toRightOf="@+id/Logo"
    android:scaleType="fitStart"
    android:background="@drawable/ImageYouWantToShow" />
Button imgButton= (Button) this.findViewById(R.id.Button)
Pang
  • 9,564
  • 146
  • 81
  • 122
Kashif Ks
  • 23
  • 4
0

Try this instead of Button

<FrameLayout
    android:layout_width="40dp"
    android:layout_height="100dp"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/prevButton"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:src="@drawable/arrow_left"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:enabled="false"
            android:clickable="false"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="prev"
            android:textSize="20sp"
            android:gravity="center"
            android:background="#00000000"
            android:textColor="#FF000000"
            android:enabled="false"
            android:clickable="false"
            android:layout_weight="1"
            />

    </LinearLayout>

</FrameLayout>
endo
  • 19
  • 5