textview cutting off a letter in android - android

this is a screen shot from my android. the text is "asd". however the "d" is slightly cut off. here is the relevant view:
<TextView
android:id="#+id/stuff"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/other_stuff"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="italic" />
any idea as to what is causing this ?

ended up with a hacky solution which is adding a white space after the last italicized character

Ok this is pretty strange but I changed from android:maxLines="1" to android:singleLine="true" and now the text is not getting cut off.

This is caused by Android setting the TextView clipping rectangle for standard text and you using italic text. Italic text leans to the right, outside of the text bounds. For some reason Android does not account for this.
To prevent this, you can force a TextView to draw outside of its bounds by giving it a text shadow. If you set the shadow color to transparent, the result is simply un-clipped text. Add these to your TextView:
android:shadowColor="#00FFFFFF"
android:shadowDx="48"
android:shadowDy="0"
android:shadowRadius="1"
Also, if this doesn't work, try setting android:clipChildren=false on the parent layout.

I fixed it with setting the width of TextView to fill_parent instead of wrap_content...

This is my solution:
Format textview and measure. After that set width of textview with 1 pixel add to the width measured.
TextView textView = new TextView(this);
textView.setText("Text blah blah");
textView.setTypeface(typeface, Typeface.BOLD_ITALIC)
textView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
textView.setLayoutParams(new LayoutParams(textView.getMeasuredWidth() + 1, LayoutParams.WRAP_CONTENT));
Working for me. Hope these help.

To narrow down the source of the error try setting android:letterSpacing="0" This was the reason for cut off letters in my case. Another hint for too much letter spacing is that the cut off part get bigger when the text has more letters in it.

You can use .
android:layout_width="fill_parent"
istead of
android:layout_width="wrap_content"

I suspect the issue would not occur if you were not using italic text. I'd test that out first, and if using non-italic text renders it correctly, then it looks like a rendering issue, that would need working around with extra padding in the TextView to allow space for the italic letters.

the problem here is, the italic property. You have to set a particular width for your textview.

You could always create custom TextView that will use for example this font (cause in fact this is a problem with italic type):
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf");
setTypeface(tf);
More details here.

I encountered the same problem but with EditText when used some fonts. My solution works also with TextView:
Use padding
As TextView using Canvas.clipRect(float, float, float, float) in onDraw method to crop - create custom Canvas class and override this method (leave it empty).
Next сreate custom TextView and override two methods onSizeChanged and onDraw.
In onSizeChanged create bitmap with the size of TextView and our custom Canvas.
In onDraw first draw in bitmap by passing our custom Canvas to method super.onDraw. After that draw bitmap to target сanvas.
More detailed in my answer to the similar question here

Use this code:
textview.setShadowLayer(25f, 0f, 0f, 0)

Simple solution If you dont want to show ... to the end of text and want to show the complete text irrespective of number of lines than just add padding bottom 5dp to the textview.

Related

TextView with 3 lines has different height depending on amount of filled lines

I have the following TextView:
<com.mycompany.CustomTextView
android:id="#+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="3"
android:maxLines="3" />
This results in an expected look of the TextView with different amounts of text:
However, when I use our corporate font created by our company, the TextView is getting bigger with every line with no text:
I don't know how this can be dependent on the used font. Is there any attribute in the .ttf file that leads to this odd behaviour?
My first thought was that Android adds new paragraphs to the TextView if not all of the lines are filled. But it would be new to me that a font file can define a default paragraph spacing.
As your view has height and width as "wrap_content" with max_lines = 3. Now it's quite obvious that view's dimensions will change depending as it will figure it out that how much height it will need to accommodate three lines of specific type (font) of input text
As you have custom Textview.Calculate and update LayoutParams dynamically based on the aggregate height of each line.
Yes Text size will vary based on font types.
Check this for detail explanation.
Vote if it helps.
Thanks. Happy coding.

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"

Can I scale the actual text in a TextView in Android?

Is it possible to create a scaled TextView like this where the text itself is scaled in one direction? In the picture, the top half shows a basic TextView outlined in blue. The bottom half shows the same TextView after the scaling I'm trying to do. The height is the same but the width of the view has been cut in half.
I don't think that this is possible with a TextView using the default font. To accomplish the effect you are looking for you would probably have better luck creating and resizing an image or dynamically using a different font that has half the width per character.
I personally use the library autofittextview from grantland in my projects.
The usage is simple.
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
for example. The result is as follows.

How can i set axis unit label?

I'm using android graphview 3.1.1. I am trying to set an axis label, a label identifying what the axis is.
The only thing I've found close to this in Android GraphView is setting individual tick-mark labels as withgraphView.setHorizontalLabels(horlabels). Is it possible to set the entire label?
Example here, the axis labels are Month and Weight in Kg.
I have had a similar problem, so I set a by -90° rotated TextView beside my LinearLayout where i draw the graph.
<TextView
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:id="#+id/test"
android:textColor="#FFFFFF"
android:gravity="center"
android:rotation="-90"
android:textSize="12dp"
android:text="°C"/>
The problem with this method: The width of my rotated TextView stays the same like if's not rotated. So there is a huge amount of space to the left and right if you use a long text. If you shorten the layout_widht there is a line break.
For short text it looks nice: http://i.stack.imgur.com/KWMk8.jpg
there is no build-in feature for that, but you can achieve this by nesting the GraphView-View in layouts with TextViews
There are no ways provided to set the entire label. You may have to do this with TextViews.

Is there a way to perfectly center text?

I wanted to know if it was possible to create a custom TextView that centers text perfectly, no matter what the font is. That's my major issue right now, but I'm also wondering if it's possible to set the specific height using pixels so that the height would also be consistent.
This picture shows how different fonts are sized and centered. The longest black line in the picture is the middle of the white space. Letters in the picture are the same in every way except for the fonts. The text size is the same (text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 450);), and they're all centered. I hope someone knows the answer to these questions! If you need any code from my app, just ask. Thanks!
EDIT: I am aware of android:gravity="veritcal_center", but that only works to an extent. What you see above is that implemented in the textview, but each font has a different center of gravity, so android:gravity="veritcal_center" wouldn't really make all of these center perfectly along the screen. I'm looking for a way to create a custom textView that somehow measures the height of text and centers it with those parameters. A suggestion by #vmironov in the comments works, but only if the textview has one character. I have not been able to mess around with his code, but I will when I get a chance and I'll post here if I find anything. Thanks!
A simple way to achieve what you want is to use following code:
<LinearLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/your_text" />
</LinearLayout>
set the height and width values of LinearLayout fixed if you want to set the text to be alligned in center within constant height and width
Set the gravity of the view to "center_vertical". Something like :
android:gravity="center_vertical"
You should be able to center your text by applying a gravity attribute to the containing TextView.
In XML you would assign an attribute to your TextView, which would look like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/some_text" />
If you do want to specify a particular size for your text, it is strongly suggested you use SP (Scaled Pixels) as your unit of measurement, and not Pixels. You could also set an Appearance Attribute for your TextView to control the size, which is also shown in the code example.

Categories

Resources