How set the android:gravity to TextView from Java side in Android
I can use android:gravity=»bottom|center_horizontal» in xml on a textview to get my desired results, but I need to do this programmatically. My textview is inside a tablerow if that matters in a relativelayout .
But if I understand correctly, that would apply it to the tablerow , not the textview?
7 Answers 7
Kotlin version (thanks to Thommy)
Also, are you talking about gravity or about layout_gravity? The latter won’t work in a RelativeLayout.
This will center the text in a text view:
We can set layout gravity on any view like below way-
This is equilent to below xml code
You should use textView.setGravity(Gravity.CENTER_HORIZONTAL); .
Remember that using
won’t work. This will set the gravity for the widget and not for it’s text.
This will set gravity of your textview.
Solved this by doing a few things, first getting the height of my TextView and diving it by the text size to get the total amount of lines possible with the TextView .
After you get this value you need to set your TextView maxLines to this new value.
Set the Gravity to Bottom once the maximum amount of lines has been exceeded and it will scroll down automatically.
In order for this to work correctly, you must use append() to the TextView , If you setText() this will not work.
The benefit of this method is that this can be used dynamically regardless of the height of your TextView and the text size . If you decide to make modifications to your layout this code would still work.
Источник
Compound Drawables
I recently learned about a small feature in Android which has been there since API level 1 (the variant that we’ll use has been in there since API 3, but others appeared in API 1): Compound Drawables. In this article we’ll have a look at what they are and see how we can use them to simplify some of our layouts.
Before we start, a little background: Recently I was working on a project where one of my layouts contained typical “icon” configuration (consisting of a graphic with a text label below it), quite similar to what we used in the article on Intelligent Layouts. My layout worked perfectly well, but I updated to ADT 16 and the new Lint tool gave me a warning which I didn’t understand: This tag and its children can be replaced by one
and a compound drawable.
I my code to be free of warnings because they are often indicative of a potential bug or inefficiency, so I wanted to learn what this warning meant in order to eradicate it. First let’s have a look at the block in my XML which caused this warning:
Which looks like this:
The warning was given on the LinearLayout element. A quick dig through the documentation for TextView lead me to the setCompoundDrawableWithIntrinsicBounds() method which is a method of attaching drawables to a TextView. We can replace the LinearLayout and its two children with a single TextView:
Then in the onCreate() method of our activity we can assign a drawable to appear above the text:
setCompoundDrawablesWithIntrinsicBounds() takes four parameters representing drawables which will be placed to the left, above, to the right of, and below the text respectively. In this case we only want an image above the text, so we set the set the second parameter to the drawable we require and the others to 0. There are a couple of variants of the setCompoundDrawables*() method which I won’t cover here, but are easy enough to understand. In addition you can use the setCompoundDrawablePadding() method to apply padding to the drawables.
I won’t bother with a screen capture of this because it is identical to the previous one – it just uses a single Widget in the layout compared to the initial one which used three.
As ever in Android, there are multiple ways of achieving the same result, and we can achieve the same thing purely in layout so that there is no java code required:
This is almost identical to the previous solution except that we have replaced the id attribute with a drawableTop one. drawableTop is effectively calling setCompoundDrawablesWithIntrinsicBounds() for us, so removing the need for any java.
As well as the simple icon layout that I’ve demonstrated here, there are plenty of other use cases where compound drawables can simplify your layouts. If you use the Lint tool in ADT 16+ you will be alerted to instances where you can make this optimisation. It my only sound like a small improvement to create one Widget instead of three, but if we expand this to the layout that we created in Intelligent Layouts, we would be creating 18 fewer widgets.
This optimisation can also be used in the Views that we create for individual ListView items to make your scrolling smoother – fewer object creations speeds up layout inflation (but always recycle your views as discussed here).
The source code for this article can be found here.
Источник
How to set drawable in Top-Left in a textview?
This is my textview. I want to set the drawable on left of this textview. But the problem is, If the text is multiline, then drawable appears center-vertical in the text view.
Is there any option to set drawable on Top-Left of the textview?
6 Answers 6
No, their is no any direct option to set drawable on Top-Left of the textview.
But you can create some custom design to achieve this. Replace your TextView with the below code
Make Custom drawable class by extending a drawable class and use for drawable .eg:
If a programmatic approach is suitable, You could extend Drawable in such way that it occupies the full height available and draws on top.
If You want an solution using only interface builder, You should just wrap the TextView in a horizontal LinearLayout, add an ImageView and set ImageViews gravity to be TOP.
Try this in xml file
change android:drawableStart to android:drawableLeft
Источник
How to get gravity ‘bottom’ working on a drawable in xml
I have a simple aim. I want a light grey background on my FrameLayout with a black dividing line underneath it (only undernearth, not all around). So far I have this:
But it draws the line through the centre, i.e. ignores the gravity=’bottom’ bit. How can I fix this?
EDIT: This question is very old. The selected answer may or may not be in-line with current APIs, etc. You are encouraged to start your own question rather than add to this one, which is no longer monitored.
7 Answers 7
I’ve struggled a lot with this as well. Basically, to create a border at the bottom, you have to think in opposite direction from what you expect. You start with a rectangle with your Border color. On top of that, you draw the actual color, with an offset from the bottom. I use this:
Unfortunately I have not found a way to get the gravity working for drawables, but I found how you can achieve what you want with pure xml:
I’d recommend using a nine-patch image for this — here’s an example that should do the job:
(It’s only tiny but it is there!)
If you place this in your drawable folder and set your FrameLayout’s background to it then you should get the desired result. The content will go in the grey area and the #010101 pixel will be stretched horizontally to form a line at the bottom. Just make sure to keep the .9.png extension to ensure it gets loaded as a nine-patch.
Источник
Is it possible to scale drawableleft & drawableright in textview?
I have TextView with drawableLeft & drawableRight in List item. The problem is, whenever the height of TextView is larger, drawableLeft & drawableLeft didn’t automatically scale based on the height of the TextView .
Is it possible to scale the height of drawableLeft & drawableRight in TextView ? (I was using 9 patch image)
10 Answers 10
This might help you out. There are two properties scaleX and scaleY
The code below will scale down the image and the text with 30%. Therefore you have to increase the font size with that many «sp», so that when it get re-sized (scaled) it would fit the «sp» you prefer.
Example. If I set the font to 18, then 30% out of 18 is 5.4sp, so roughly, this is the value I am targeting at, because when it gets scaled, it would become 13sp
The last thing to do is set the CompundDrawable.
I solved an equivalent usecase by introducing a ScaleDrawable and overriding its .getIntrisicHeight() so that it is at least the TextView height. The TextView.addOnLayoutChangeListener part, required to rebind the Drawable on a TextView size change works with API11+
The only acceptable answer here should be to use an ImageView with the scaleTypes as per usual. Hacky work arounds to scale an image on a TextView that isn’t supported by Android seems.. unnecessary. Use the SDK as it was intended.
Wrap your resource in a drawable that defines your desired size similar to:
After that, use this drawable in your textview tag
I did not found way. But that you show looks like a list of items. Each item would be a LinearLayout horizontal with imagesView from left to right and text in center. The image dimension do a android:scaleType=»fitXY» for ajust the size height to the textview height. If you want no deform de image use scaleType=»CENTER_INSIDE». http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Another approach to create a custom size for the drawable inside the TextView is by using the BindingAdapter. Using this, you will be able to set you image size in your xml.
For example, TextViewBindingAdapter.java
Now, in your .xml you can set the android:imageSize.
And don’t forget to set in your gradle.build this code below:
Just make 9-path background repeating this pattern.
And also it seems it will be better look in case the pattern will be applied to the list, not to individual item.
I think the cleanest solution would be to override TextView . Here’s an example:
I changed a bit:
This way you can set the size of the drawable but if you care about width/height ratio make it the way suits you.
Now if you want it to automatically match height of the textview you may also override the TextView ‘s method onSizeChanged :
You my also want to declare sizes for each side, for example leftCompoundDrawableWidth, rightCompoundDrawableWidth and so on.
Note that it does not work with a Shape as drawable. Sounds like TextView only accepts it if it has the size attributes, that way one can implement it by updating the size of the shapedrawable using setSize or setIntrinsicWidth or . Not tested.
Источник