Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the text in ImageButton

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

like image 636
Suniel Avatar asked Feb 14 '14 10:02

Suniel


People also ask

How to set text in ImageButton in android?

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)) .

Can we add text to ImageButton?

Answer: You can not display text with imageButton . Method that tell in Accepted answer also not work. If you use android:drawableLeft="@drawable/buttonok" then you can not set drawable in center of button . If you use android:background="@drawable/button_bg" then color of your drawable will be changed.

What is image Button?

Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.


1 Answers

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

like image 99
Nitesh Tiwari Avatar answered Sep 22 '22 10:09

Nitesh Tiwari