Set actual text padding in TextView with compound drawables - android

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

Related

Add padding for each cell in RecyclerView

I have a RecyclerView with a background color of gray, in the RecyclerView, I populate it with 3 different types of cells, with background color white.
I want to add padding to the left and right for all cells.
If I add the left and right padding to the RecyclerView however, it makes the background look funky and it shows gray bars since the padding is for RecyclerView rather than the cells.
Is there any way to do it without adding padding to each type of cell?
Yes adding the padding to each cell is certainly ok.
If you want it to be easily maintainable and for all values to be the same. Add a padding resource to dimens.xml
<dimen name="recycler_view_h_padding">16dp</dimen>
And then apply the padding to each cell with the reference
android:paddingStart="#dimen/reycler_view_h_padding"
android:paddingEnd="#dimen/recycler_view_h_padding"

Android: Placing a TextView centered around a specified position in XML

I have a TextView that can have a few different values, and is updated runtime (in Java code).
However, I need this TextView to retain its center point, so that when the text in that TextView is updated, it is always center justified. It should be centered around a point which is not the center of the screen or anything else, so setting gravity only will not help.
As the values that it may contain are already defined, I could try with the longest one first, position it to the correct top left position and set its gravity to center. In this case every shorter in length text should fit correctly.
However, I would like to know if there is better approach, for cases when the values are not known beforehand.
This TextView is placed below an ImageView and it could take the whole screen width (nothing else is placed left or right to it).
Note: I guess it could be also possible to position it every time to a new X axis position, whenever the text is changed, but I don't think it is a nice solution at all.
Set the android:layout_width to match_parent and set android:gravity to center_horizontal.
This way the View is stretched all the way horizontally, and the content (the text) will be centered. If the content changes, it will still be centered.
One thing that many people don't realize is the difference between android:gravity and android:layout_gravity. The first one defines the alignment of the content inside itself, the other one defines alignment of itself relative to its parent.

How to align the text with its compound drawables in TextView?

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.

Android ellipse overflow priority

I have 2 textviews per horizontal linearlayout row on two rows. All the views are set as 0dp width and weight 1. They all have the same font and text size etc.
The views on the left side are gravity aligned left and the two on the right are gravity aligned right.
When both textviews text length overflow android always gives precedence to the textview on the right and ellipse the views on the left.
Is there a method that can be used to control which view ellipses when both views on the same row would not fit.
Ideally I want the views on the right to ellipse in favor of those on the left. Or failing that make them ellipse evenly per row.
thanks
i don't think there is a feature of order of the views to manage how they are measured.
you can customize the linearLayout by extending it in order to support this feature , but this is too hardcore for this task .
i would suggest putting the problematic views (those that take too much space and you don't with them to take too much space) into a new layout , and set its width to match_parent .
this way , it should take the rest of the space at the end of the measurements of the other views ,

Android - spinner default margins

I am trying to center a spinner vertically, but it does not work since the spinner seems to have default margins that are not symmetric (the bottom margin is a bit larger than the top margin). If I set any margins to the spinner element, they are added to the default margin.
What is the recommended way to center the Spinner element vertically?
I think that bigger bottom margin exist because of the drawable(9 patch PNG) used for the spinner(you can check the drawable in the SDK):
A solution to this is to make your own spinner's drawable(9 patch PNG) that has equal space on top and at the bottom.

Categories

Resources