If a TextView
does not have enough space in its parent element, I will show an icon. A tab on that text or the icon will be used to call an alert dialog with the full string. So i need to know if a TextView
has been trinmmed.
Asked
Active
Viewed 4,915 times
2 Answers
5
Claculate the width of TextView and also calculate the width of text which wil be displayed in the textview. If the width of text is more that the width of textView that means your have to call dialog because because the text will be marqued. Otherwise the text completely fit in the TextView without any issue so no need of dialog box.
use the following code.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// System.out.println("...111Height..."+mainLayout.getMeasuredHeight());
isMarqueed("I am fine here. How r u", textView.getWidth(), textView);
isMarqueed("I am fine", textView.getWidth(), textView);
}
private boolean isMarqueed(String text, int textWidth, TextView tv) {
Paint testPaint = new Paint();
testPaint.set(tv.getPaint());
boolean isMarquee = true;
if (textWidth > 0) {
int availableWidth = (int) (textWidth - tv.getPaddingLeft() - tv.getPaddingRight()-testPaint.measureText(text));
System.out.println("...available width..."+availableWidth);
// tv.setText(text);
isMarquee = false;
}
return isMarquee;
}
Thanks Deepak

Sunil Kumar Sahoo
- 53,011
- 55
- 178
- 243
-
Thanks @DKIT Android and @Deepak for the fast reply, your solution sounds good. – Webworx23 Jun 01 '11 at 06:51
-
3How is this the accepted answer? The condition `textWidth > 0` is always going to be true if you pass a proper width. Aren't you supposed to check the `availableWidth` variable in order to set `isMarquee` variable to true or false? @SunilKumarSahoo – Vignesh PT Sep 02 '15 at 09:52
0
Try using Paint.getFontMetrics to decide how much space the text needs, then calculate if this will fit inside the TextView.

DKIT
- 3,471
- 2
- 20
- 24