How to center a multiline text view with the smallest width necessary? - android

I would like to have my 2-line text view in android-xml centered horizontally with the smallest width necessary. At the moment it's like the first line is completely filled and the second with the rest of the words.
Does anyone know how to achieve that both lines are filled kind of equally? Below are 2 images, the first showing the default multi-line text view, the second what I would like to achieve. As you can see, the second image requires much less width than the first and is more in the center of the view.
Thanks in advance!

Try setting android:breakStrategy attribute of textView to balanced
android:breakStrategy="balanced"
Add this line to your textView's xml code, this will balance the lines.

Related

90º rotated textview gets size to display text as the horizontal space

I have a textview where I want to show text in vertical (just like rotated 90º). With android:rotation="-90" you can get this.
If the text would have no rotation, and you would like to fit a sentence, you would first look at the width of the textview, since text will try to be in 1 line. Since I rotate the text, here I expect to see the height of the object. However, Android is still considering the width. I need to fix the size of the width, but this causes me a problem for the text: Although the textview has a big vertical dimension and text would fit, Android thinks it needs to use the width to calculate the available space. At the end this causes me overflow of the text, appearing for example, just one word as multiple lines of 1 char.
How can I make android to understand that the space for the lenght to fit the text is now the vertical, instead of the horizontal?

Is there a way to set min-height of textview wrt to lines?

Is there a way to set the min-height of a text view wrt to the number of lines. Let's say if I know that the text size is 20dp and the min-height should be such that it's equivalent to 3 lines of text then what should it be. Could be done by using line height or some hacks but I wanted to know if there's a legit way to achieve this?
You should add TextView height to wrap_content and ellipsize to end.
android:minLines="3"

TextView onDraw - Drawing Lines

I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to
A|B|C|D
without the use of the | character and use drawLine() instead.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.
Not sure whats not working, help appreciated.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.
That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.
Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.

Proper layout for EditText Boxes

I was wondering about what is wrong with my layout.
Here is a screenshot.
Problem being that the text is being cut off in edit text boxes.
I suppose there is something wrong with the text's XY co-ordinates at the time of typing in those edit text boxes; the cursor blinks fine to me.
Here is the pastebin of my XML layout (not putting it here because its quite lengthy).
Any advice will be appreciated. Thanks
The text size of the text inside edittext is large for hardcoded 30dp height of edittext. You can either set edittext height to wrapcontent or reduce the textsize of edittext (10-14sp)so that it fits inside.
It is advised by android to set minimum height of clickable area to at least 48dp.
http://developer.android.com/design/style/metrics-grids.html

Vertically centering overlarge text in Android buttons

Android, Button view. The Button size is 38x38dp, text size is 20dp (just one + character worth of text). Gravity is set to center|center_vertically.
The text is NOT centered vertically. According to Android's internal accounting, the text is too large for the specified button size, so the text is placed with its top aligned with the top padding, and its bottom cut off. That's not what I want; I want the text to be cut evenly on the top and on the bottom. In other words, vertically centered. The plus character, since it takes less than a full-sized character cell, won't suffer from that.
It's Gravity that I set, not Layout gravity. I know the difference.
Any ideas how to make vertical centering work in such conditions, short of overriding draw()?
Try adding android:includeFontPadding="false" to the Button view.
Some have suggested adding android:baselineAligned="false" to the container for similar problems but it didn't do anything for me.

Categories

Resources