2

I have a custom ListView. inside a custom ListView, I have a TextView & it's look like this

enter image description here

Now, my question is

1) is there any way to check If names is not to wide to fit onto the screen in Android ? I mean I don't want two lines. Like here Wachler is on 2nd line. So, how do I check if all names are wide to fit onto the screen in one line.

Adnan
  • 8,468
  • 9
  • 29
  • 45

6 Answers6

2
//1) 
layout_width = yourLayout.getWidth(); //in order to measure the width of the ListView rows container

To find text size:

//2)
Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height =  bounds.height();
text_width =  bounds.width();

//3)

if(text_width < layout_width){
   //..do your magic
}

Source

Community
  • 1
  • 1
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
2

you can set your custom listview layout's TextView property as android:ellipsize="end" and also set the android:singleLine="true" in your TextView.

The TextView will use an ellipsize when it can not expand to show all of its text. The attribute ellipsized sets the position of the three dots if it is necessary.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
1

I've solved this issue by checking how many lines does the TextView have after onSizeChanged finish, since only then you will have the real value of the number of lines.

First create an interface (you can define it on your desired Fragment/Activity)

public interface OnCustomTextViewSizeChangedListener {
    public void SizeChanged(int size);
} 

Then create your custom TextView, Override onSizeChanged and create a function to define your listener.

public class CustomTextView extends TextView {

    private OnCustomTextViewSizeChangedListener sizeChangeListener;
    ...

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        if(this.sizeChangeListener != null){
            this.sizeChangeListener.SizeChanged(getLineCount());
        }
    }

    public void setSizeChangeListener( OnCustomTextViewSizeChangedListener listener){
        this.sizeChangeListener = listener;
    }
}

Then just create your listener on your Fragment/Activity, define it for your desired TextView and your good to go!

OnCustomTextViewSizeChangedListener textViewSizeChanged = new  OnCustomTextViewSizeChangedListener() {

    @Override
    public void SizeChanged(int size) {
        if(size == 2){
           //Do your stuff here, like changing text size
        }
    }
}

PS: don't forget to change your layout to use your CustomTextView

Hope it helps!

manuelvsc
  • 525
  • 7
  • 11
0

If you want your text view display in one line then do this-

textView.setMaxLines(1);

but Remember it cuts out the text which not fit in textview

T_V
  • 17,440
  • 6
  • 36
  • 48
0

I suppose you have a TextView displaying the text?

You can check if a textviews text is trimmed by calculating the width of TextView and also calculate the width of text which will be displayed in the textview:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);

    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);

        isMarquee = false;
    }
    return isMarquee;
}

Taken from here: How to check if a TextView String has been trimmed (marquee)?

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
0

Since you're using a TextView you can use it's method getLineCount() to determine how many lines this TextView expands over. But be careful when using this method, you must be sure that the view has been drawn before invoking this method. See documentation for more information.

Robert
  • 4,602
  • 4
  • 22
  • 33