How can i drop an image under a piece of text in a TextView.
For example i want to drop image of bread under the text of bread in my TextView. I really want to show a drawable instead of background color for a part of text in a TextView. Is it possible with ImageSpan?
you can just simply add android:drawablebottom="#drawable/yourimage" to the TextView in xml
or otherwise the equivalent in Java is
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)
usage:
myText.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);
Just add android:drawablebottom="#drawable/bread" in your textview
If you want it like a watermark, use ImageButton as follows, make sure you use a small icon-like image
<ImageButton
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="#drawable/bread" />
Write below code and try
In your XML File
<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:src="Bread" android:id="#+id/mBtn1" />
In your Java File.
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
Drawable d = getResources().getDrawable(R.drawable.bread);
mBtn1.setCompoundDrawablesWithIntrinsicBounds(null, null, null, d);
it will solve your problem.
Related
I have an icon I would like to scale down to 48dp and use as a button (no background). However, adding the android:background attribute (to make the background transparent) seems to break the sizing I have applied to the button and it displays at its full size:
Desired size but with ugly background:
<ImageButton
android:id="#+id/composition_send"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:scaleType="fitCenter"
android:src="#drawable/ic_send_black_48dp"/>
Full (wrong) size, but with no background (desired):
<ImageButton
...
android:background="?android:selectableItemBackground"/>
Why does adding that one line ruin the sizing?
try to add
android:src="#drawable/ic_send_black_48dp
android:backgroundTint="#0000"
to ImageButton, it will make the background transparent and then you can set the image on it.
When you don't set the background property for the Imagebutton, the Imagebutton will use the default Imagebutton background of Android. And this background contains the padding so your image is smaller.
So when you set the background for Imagebutton by a color or resource that doesn't contain padding, your image will look larger
You can find the default background of the Imagebutton and another View in
..\Android\sdk\platforms\android-23\data\res\drawable\
(Some resource in this is not public)
For example
If you compile your project with API 23, the default background of ImageButton is btn_default_material
<ImageButton
...
android:background="#android:drawable/btn_default_material"
android:src="#drawable/ic_send_black_48dp"/>
<ImageButton
...
android:src="#drawable/ic_send_black_48dp"/>
So you set the android:background="#android:drawable/btn_default_material" or don't use background property, 2 ImageButton will look same (note: btn_default_material is not public for use)
Hope this help
If you don't want that ugly background in imagebutton,then you can use an imageview as button and set onClickListener on it. It will behave like a button.
This Link will be helpful to you.
I need to draw some divider line with some background image. I tried the ImageView control with the image as the value of android:src, but it didn't work. So I replaced the ImageView with a TextView with no text inside and specified the background image as its background. It indeed worked. Is there any better solution besides using a TextView?
Why not use the View Widget?
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#drawable/fixed_divider_horizontal_bright" >
</View>
NOTE: The dot you see right above this, is a 9-patch image (used in the code snippet). ;-)
i always just use a View and set the backgound.
I don't exactly understand your need. If you want use the ImageView as a divider, you can try this attribute android:scaleType="fitXY"
i can set image textview rightside using xml attribute like
<TextView
android:id="#+id/txtarrow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/arrow"
android:gravity="right"
android:onClick="VideoClick"
android:padding="5dip"
android:textColor="#feb4e7" >
</TextView>
i want to set
android:drawableRight="#drawable/arrow"
programatically
like
txtarrow1.setBackgroundResource(R.drawable.arrow_blue);
but this one is not working
You can simply use Google to find the Android java docs of TextView.
They have a huge table showing every attribute's its programatical option.
android:drawableRight (#compile time)
setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) (#runtime)
The drawable to be drawn to the right of the text.
Here You want to use
Adding image on specific direction in Text View pro-grammatically
There are 2 method :
1> Through Image Drawable
ex:
Drawable img = getContext().getResources().getDrawable( R.drawable.dogy );
txtarrow1.setCompoundDrawablesWithIntrinsicBounds(
img,// left
null,//top
null,// right
null//bottom);
Remember this method is most use-full in image store in one view (Text View , Button)
2> Through Image Id
txtarrow1.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_doggy,// left
0,//top
0,// right
0//bottom);
Most probably these method use in different images display in multiple view like (GridView)
Must remember difference between these two when Drawable can't show image set null & in ID set 0 (zero)
You can use SpannableString..
SpannableString text = new SpannableString("text");
text.setSpan(new ImageSpan(getActivity(), R.drawable.arrow_blue), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
txtarrow1.setText(text);
For EditText that going be filled with RTL text, is there a way to change the gravity of the error drawable (and popup of course) ?
here is an example of regular error drawable
so since the text entered is RTL i would like the pop up to show up at the LEFT side of the EditText
i tried to apply custom drawable , but Drawable doesnt seem to have any setGravity method.
thanks in advance.
It's not possible by normal means, however you can extend the Android TextView class and change the layout that Android uses to inflate the popup. I haven't tried this myself but it could work.
I did a quick look in the source of TextView and found this line
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null);
Which references to this layout.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/popup_inline_error"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
/>
Adding gravity here could do the trick. A similar approach for the drawable could apply.
It's over a year, but if it helps anyone else, you can use:
myEdit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_error, 0, 0, 0);
but it will stay until you remove it
myEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
I've been working on my android calculator and i can't seem to insert an image instead of text on a button. For example sqrt I don't want to have sqrt written on my button i want the image of the symbol on it, the same goes for x^y and alot of others. I have my custom background of the button working just fine. Thank you for your help in advance :)
EDIT:
Thats not what i wanted i did manage to get my custom button and thats fine. On my button since its a calculator button i have android:text on it saying sqrt. I want to remove that and insert another png on my button showing the symbol of the square root instead of that text. Thank you for quick responses.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/imageName"/>
You can have both text and image (or no text) when using the attribute drawableTop, drawableBottom, drawableLeft and drawableRight.
And for positioning the image how you want, consider using: paddingTop, paddingBottom, paddingLeft and paddingRight.
You can do this by ImageButton:
<ImageButton
android:id="#+id/submitEmailButton"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="right" android:layout_marginRight="15dip"
android:background="#drawable/ic_submit" />
Here ic_submit is the image you want to show as a button.
I used this tutorial and it worked perfectly for me: http://www.mkyong.com/android/android-imagebutton-selector-example/
I haven't seen your code but basically you should remove the "text" tag from your xml and it should appear the image without text then.
I don't know if that's exactly what you were looking for or not, but I reccomend you to follow the tutorial and for sure it will work for you ;)
There are a number of alternatives you can try to implement to build your SQRT button:
draw the button as a 9-patch drawable so your SQRT symbol won't get distorted;
use a RelativeLayout with a button inside and implement a selector
You can simply set background for the Button and keep its text blank.