Different TextView height when not all lines have content - android

I have a TextView with lines = 2. I get a different result in the height of the TextView if I set a short text and only 1 line shows up, or I set a longer text and 2 lines show up. This seems to be an issue with the onMeasure function of TextView. In the code I can see that first the height of the Layout is calculated, and then a line height is added for each blank line. Javadoc notes the following:
Note that markup within the text can cause individual lines to be taller or shorter than this height, and the layout may contain additional first-or last-line padding.
These TextViews are used in a horizontal RecyclerView where I need each list item to be the same height, that's why I set the lines fixed to 2. But apparently that doesn't make it the same height. How can this be fixed?
Some extra info regarding the TextView:
Font: Helvetica Neue Lt Std Bold
Text size: 18 sp
Lineheight: 32 sp
Either it has something to do with the lineheight not being used in the calculation of getLineHeight or Layout. Or something with the font itself. Edit: it's definitely something with the font because lineheight + roboto doesn't create this issue.
Here you can see the difference in height:
And here the TextViews have the same height because they all have the same number of 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?

Approximating "height" of layout when contains TextView

I want to use this simple layout for items of list:
Note the right gray area (which I draw red rectangle on its border in below) must be a square:
So I have to approximate the height of layout. It must be simple, but the problem is that the text size of TextViews is set by using sp unit and height of layout must be set by dp unit and I did not found a way for converting dp and sp in .xml file. Also I do not want to create a CustomView or change properties of layout at runtime. All things must be done in .xml file. How I can approximate height of layout?
See the TextSize is different from TextView's width and height.
So Provide fixed width-height to all views in "dp". Fixed Text-Size in "sp" that fits perfectly inside TextView.
P.S. -> test in a few devices/emulators to verify.

How can I get the size in pixels of the space below the text of a textview? (not the bottom padding)

bottomPadding returns 0 and is set to 0, but my textview still has padding below it's text.
I am trying to draw text on a canvas at the same height as the textview is from its parent but for some reason Paint.drawText() ignores the extra padding at the bottom.
Since the canvas object has its origin a the bottom-left corner, I have to provide the the y position of the text from below.
I can't do the following:
yPosition = textview.bottom + ((rectbounds.getHeight() - textViewHeigth)/2)
This is because the size of the space on top of the text is bigger than the bottom space.
Removing the extra space from the textview would also solve my problem. However, I already tried setting includeFontPadding to false, and setting padding to 0, but neither work.
What happens if you put a 'g', 'p', 'y', or 'j' in your TextView? Each of these characters has a descender, and the metrics for the font will always allocate space for this feature of many scripts. This is to allow for the fact that multiple lines of flowing text ought not be crammed into each other.
If you really need to get the size of a font's descender, you can use a Paint object, load it with a Typeface and ask for its descent(). But a TextView I don't think is going to change the way it renders its lines for you. If you must ignore the descent, draw your own text in a custom view.

Android - Button height not decreasing to wrap text

I set the button's height via XML. Suppose it to be X
<Button
android:layout_height="wrap_contnet"
android:textSize = " <X> dp"
....
....
/>
If I set X to greater than 15 dp, the button's height grows to fit the text. But when I set it to something lower like 2dp, the button's height doesn't change and I can see a thick border around it as shown below:-
As shown above, text size is so small but still height doesn't decrease!
How can I force it to be of appropriate height?
Thanx in advance!
Font size does not depend on button height. If you set it too small to fit the text, you will get the result you observe
EDIT
wrap_content would usually do the trick, however Button class sets some layout parameters like margin, background so it takes more space. You may get rid of Button and use i.e. TextView class, styled by hand to match your design. onClickListener will work perfectly fine with it and you will get result you want and full control over your button
EDIT 2
As this is bit related - there's AutoFitTextView widget on Github that deals with automatic text size scalling: https://github.com/grantland/android-autofittextview
Just Set android:minHeight="xxdp"

Resizing the textview size based on the screen width for all devices

I have problem in resizing the text view .
I have a Layout like this with below components
ImageView| TextView | ImageView
placed in relativelayout(horizontal) and each one width is wrap content and height is fill parent.
PROBLEM
I have a text like this "My name","My name is xyzmnop" now, i have fixed the TextView size to 15sp, so what is happening is in larger devices since Textviews width increases the text "My name is xyzmnop" will fit but in smaller devices since the width is small it will spill out to second line. But i want it to be in same line, as I have the flexibility to decrease the size of the text if the text is lengthy.
please help me with this problem.
i have seen this
Android TextView doesn't resize after changing text size
Android EditText Resize Programatically
Resizing TextView does not shrink its height on Android 3.1
EDIT:
I have also added setSingleLine = "true"; in my xml
Say, tv is your TextView.
Just call tv.setMaxLines(1);
TextView has the setMaxLines (int maxlines) method:
Makes the TextView at most this many lines tall. Setting this value overrides any other (maximum) height setting.
I suppose this might help you if you set the maximum number of lines to 1.
Note: there is also the android:maxlines attribute if you need to set it in your XML layout.
i have solved it using ViewTreeObserver.

Categories

Resources