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 ...
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)) .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With