Found another solution (tested on 4.1.2 and 4.3 while using wrap_content
). If you extend TextView
or EditText
class you can override onMeasure
method this way:
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int measuredWidth = getMeasuredWidth();
final float tenPercentOfMeasuredWidth = measuredWidth * 0.1f;
final int newWidth = measuredWidth + (int) tenPercentOfMeasuredWidth;
setMeasuredDimension(newWidth, getMeasuredHeight());
}
Looking at the source I found out that setting a shadow extends the clip rectangle.
A trick is to set an invisible shadow just beyond the character.
For example:
android:shadowRadius="2"
android:shadowDx="2"
android:shadowColor="#00000000"
I think this solution is better as it will not extend the width of the TextView
which may happen when adding an extra character (which is more apparent with a background).
android:layout_width="wrap_content"
, gives you a rectangle for wrapped content rendering.
All will work well for normal text (non-italic).
Once you have italic
text enabled, the wrapped text will try to fit into the rectangle and hence the rightmost character will be cut off unless its un-cut-able (such as .
, )
, 1
, etc.)
Solution as suggested is to have a space at the end of the text (or anything better ??)
PS: This applies to android:gravity="right"
too because the text will be pushed to the rightmost. If we have italic
text, we face the same problem.
You can add a HAIR SPACE to your string
<string name="hair_space"> </string>
String hairSpace = getContext().getString(R.string.hair_space);
textView.setText(hairSpace + originalString + hairSpace)
Add a 3dp padding to the right on your TextView. I tried with 1dp and 2dp, but 3dp seemed to do the trick fully.
android:paddingRight="3dp"
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.