I have a textView in which I put some text (obviously) and a Drawable left. Unfortunately, when I use phones with small screens like a Samsung Galaxy Y, instead of just a single line, the text runs over to a second line. (which is fine). However, the drawable that I set now aligns itself to the center of the textview, and not on the left of the first line.
I want my drawable to stay aligned to the first line of text in my textview, no matter how many lines of text, my textview holds. Is there a way to do this? Can anyone point me in the right direction?
my implementation is pretty straightforward
...
tv.setCompoundDrawables(drawableLeft, null, null, null);
where my drawableLeft is the image from my drawable folder.
Thanks a lot!
Try to use a drawable like a separate view (ie ImageView) and not to set it as a tag in a TextView. Using that will grant you more control over the drawable/ImageView it self.
In order to align a Drawable to the top of your TextView you can use a custom Drawable that wraps the Drawable you want to show. Then, override the method onDraw(Canvas) in your custom Drawable and translate the Canvas to manipulate the position of your Drawable.
See my answer here for an example.
Related
Simple question here : I am customizing some buttons, placing an icon next to my text via android:drawableLeft or Right, but I can't seem to find how to define the size with accuracy. My image happen to be too big and go out of the shape of my button.
Oh, and while I am at it: android:drawableLeft allows to put an image on the left of the text, but how to decide the distance FROM the text ? In my case, it automatically goes to the extreme left of my button while my text is centered. The result is everything but nice !
Thanks !
I recommend to start from this topic.
but how to decide the distance FROM the text ?
Here you have an answer:
android:drawablePadding can be used to specify the amount of padding between the image and button text.
Source
i guess this will do the trick
use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button...
If you want to define the accuracy size of the drawable, You should use Java code instead of android:drawableLeft
Drawable dw = getResources().getDrawable(R.drawable.ic);
dw.setBounds(0, 0, 100, 100); // set the size of the drawable to 100*100
button.setCompoundDrawables(dw, null, null, null);
because of
android:drawableLeft // equals with setCompoundDrawablesWithIntrinsicBounds
and
android:drawablePadding // this used to control the padding between the text and drawable
I can't figure out how to set the alignment of text and its compound drawable (smaller than text) in a TextView. It seems the default alignment is center, but I need to align them by the edge.
PS: The android:gravity works great if compound drawable is larger than text, but not if smaller.
It sounds like you're using an image with fixed dimensions for android:drawableLeft.
Try using a nine-patch or defining a drawable through an XML resource file. In this way your drawable's dimensions will stretch to align in the TextView the way that you want it to.
I had a similar problem in that I wanted to use setCompoundDrawables to set a top drawable for a floating hint, however TextView (EditText) sets the top drawable to center, irrespective of what its width is.
I am using a custom Drawable, and inside its draw method, I am grabbing the clipBounds, and translating on its top,left to get back to the TextView's 0,0 position.
The other way to do it easily is to getMatrix and mapPoints from 0,0 then take the negative of them and translate back to 0,0 that way.
I have a ListView populated with an ArrayAdapter. For items I use just a single TextView layout. I want some of the rows to have compound drawables set.
Question: is there a way to set padding for the actual text that is contained in TextView so that the compound drawables don't get the padding too? Other solution would be to lock the width of text. Do I need to add ImageViews to my layout?
Quite simple:
android:drawablePadding="5dp"
It will automatically use the padding according to the direction.
I'm posting this as an answer so that someone, who comes across this post finds the answer.
There's no direct way to set a padding only to text, but you can set a positive padding to the whole TextView and set a negative padding for the drawable.
Improving on Gio's sugestion, which is probably for a dated API/SDK...
TL:DR - just apply android:drawablePadding to increase distance between image and text.
Explanation:
android:drawablePadding now applies padding between CompoundDrawable and original View, in this case the TextView containing the text;
android:padding and modifiers now apply to the whole group, including all compounds.
Doing the negative-positive hack doesn't seem to work no more.
So depending on the drawable position (drawableTop/Left/...), the padding will apply in the opposite direction of it, right next to the drawable, in other words between the two elements. For instance, Applying drawablePadding="10dp" to a left placed drawable would have a similar effect as setting an individual ImageView to the left of the TextView with paddingRight="10dp" (at least padding-wise).
I have been trying to make a circular TextView. Its a circle in which I want to accomodate whole space above a circular bubble as shown in image below.
Kindly see attached image.
In this image, we have a circular bubble with circular text in it.
I have already tried setting oval shape .xml as background of TextView but still no luck.
Edit:
As text length increase. It must reduces in size to fit inside the circle. This is the hardest part to think about.
You need to create a custom view, extending from TextView probably, setting the circle as background image, and calculate the text width / break the lines manually according to the width of the text.
To calculate the width of a string, see How to calculate string font width in pixels?
Some math and calculations is required of course to measure the available space per line; but I think that's the only way, as there's no standard component out there to do it.
To place the text onto the view, use drawText of the Canvas class.
I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.
I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?
You need to use the
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right,
Drawable bottom)
method with null for any that are not needed.
Usually you can change using this
Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
Be aware you can miss the button text.