Android: combining text & image on a Button or ImageButton
You can call setBackground()
on a Button
to set the background of the button.
Any text will appear above the background.
If you are looking for something similar in xml there is:android:background
attribute which works the same way.
Combining an Image with Text inside a Button
You can use ImageSpan to add a drawable
in between text. Below is an example .
TextView textView=(TextView)findViewById(R.id.b1);
Spannable span = new SpannableString("G OL");
Drawable android = getResources().getDrawable(R.mipmap.ic_launcher);
android.setBounds(0, 0, 50,50);
ImageSpan image = new ImageSpan(android, ImageSpan.ALIGN_BASELINE);
span.setSpan(image, 1, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(span);
Below is the output . You can modify the bounds.
Android Overlaying a text on top of an ImageButton
Try wrapping your ImageView
and TextView
in a RelativeLayout
as:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
and set android:weightSum=3
in the parent layout. Once you have the desired layout, make the RelativeLayout
clickable instead of using an ImageButton
.
writing text on imagebutton
The description of ImageButton
Displays a button with an image (instead of text) that can be pressed or clicked by the user.
Plus, ImageButton extends ImageView
in the source code whereas Button extends TextView
, meaning that you can't display text on an ImageButton
.
In that case, you can simply change your ImageButton
to Button
, and it should work.
Sidenote: You set the android:src
attribute of an ImageButton
to show an image, anyway, so you weren't implementing that correctly.
Button with text AND an image (Android)
If I were you I would just not use a Button. Use LinearLayout, or RelativeLayout and just give it the Button background(Hint: use a selector if you want it to have different images for each state) and place your TextView inside of it, then you can use the drawableLeft, drawableTop etc.. attributes to put the picture on whichever side you want. If you want a greater level of control as to where the picture goes in relation to the text then use one TextView and one ImageView. Then in your java just get a reference to your layout and treat it just like you would a button, with setOnClickListener().
Adding a text and image on a button programmatically in android
I noticed that the size of the image is very important when using it on other objects - when I adjusted the image to a smaller size, my code worked fine....
The same code that I was trying earlier worked for me after resizing the image...
Button btn = new Button(this);
btn.setTextColor(Color.parseColor("#000000"));
btn.setText("SomeText");
btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, mediumTextSize);
btn.setGravity(Gravity.CENTER | Gravity.LEFT);
Drawable icon= getApplicationContext().getResources().getDrawable(R.drawable.cal);
btn.setBackgroundResource(R.drawable.light_bg);
icon.setBounds(0, 0, 0, 0); //Left,Top,Right,Bottom
btn.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null);
Thank you all for your help and suggestions !
How can i create a BUTTON with image and text?
There are several examples if you search further. For instance:
Button with text AND an image (Android)
Android: combining text & image on a Button or ImageButton
How to show the text on a ImageButton?
As you can't use android:text
I recommend you to use a normal button and use one of the compound drawables. For instance:
<Button
android:id="@+id/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/buttonok"
android:text="OK"/>
You can put the drawable wherever you want by using: drawableTop
, drawableBottom
, drawableLeft
or drawableRight
.
UPDATE
For a button this too works pretty fine. Putting android:background
is fine!
<Button
android:id="@+id/fragment_left_menu_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
android:text="@string/login_string" />
I just had this issue and is working perfectly.
Android Button with Text with different images for Enabled and Pressed
You have to use android:dither = "true"
Try this.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true" >
<item android:drawableTop="@drawable/multiplayer" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawableTop="@drawable/multiplayer_pressed" android:state_pressed="true"/>
</selector>
From Android Docs:
Enables or disables dithering of the bitmap if the bitmap does not
have the same pixel configuration as the screen (for instance: a ARGB
8888 bitmap with an RGB 565 screen).Must be a boolean value, either "true" or "false".
This may also be a reference to a resource (in the form
"@[package:]type:name") or theme attribute (in the form
"?[package:][type:]name") containing a value of this type.
Dithering affects how colors that are higher precision than the device are down-sampled. No dithering is generally faster, but higher precision colors are just truncated down (e.g. 8888 -> 565). Dithering tries to distribute the error inherent in this process, to reduce the visual artifacts.
I hope it helps!
Related Topics
Android - Sms Broadcast Receiver
Differencebetween Min Sdk Version/Target Sdk Version VS. Compile Sdk Version
Android: How to Check If a View Inside of Scrollview Is Visible
How to Add Manifest Permission to an Application
Clicking Urls Opens Default Browser
How to Lay Out Views in Relativelayout Programmatically
Android Push Notifications: Icon Not Displaying in Notification, White Square Shown Instead
How to Handle Dynamic Json in Retrofit
Noclassdeffounderror For Code in an Java Library on Android
How to Detect Android App Uninstall
How to Pass Arraylist<Customeobject> from One Activity to Another
Android "Elevation" Not Showing a Shadow
How to Change the Floating Label Color of Textinputlayout
Firebase Firestore Variable Name Changed
Programmatically Go Back to the Previous Fragment in the Backstack
How to Modify Arrayadapter in Listview: Unsupportedoperationexception