How to use getlinecount () in android textview

I want to know how many lines are in my text view. I already set the text mytextview, then I want to get how many lines it occupies in mytextview.

I am using mytextview.getLineCount (), but this did not work. it always returns 0.

can someone help.

+8
android android-textview
source share
6 answers

thanks for all. I decided it already. I use thread to get a linecount that way.

@Override public void run() { // TODO Auto-generated method stub while(textView.getLineCount() == 0){ } countLine = textView.getLineCount(); } 

hope this helps if you have the same problem. the best way.

-8
source share

You need to send a method to count the number of rows. Here is a sample code

 imageCaption.setText("Text Here"); imageCaption.post(new Runnable() { @Override public void run() { int lineCount = imageCaption.getLineCount(); Log.v("LINE_NUMBERS", lineCount+""); } }); 
+30
source share

you can check the TextView parameters inside onCreateOptionsMenu ()

 @Override public boolean onCreateOptionsMenu(Menu menu) { TextView tv1 = (TextView)findViewById(R.id.textView); Rect rect = new Rect(); int c = tv1.getLineCount(); tv1.getLineBounds(0, rect); tv1.getLineBounds(1, rect); return true; } 
+2
source share

public int getLineCount ()

Because: API Level 1

Returns the number of lines of text or returns 0 if an internal layout has not been created.

+1
source share

Please check this link.

Android Edittext: get LineCount in onCreate () for activity (activity)

tv.getLineCount () will always be retirn 0 if the internal layout has not yet been created.

+1
source share

The following will provide the number of lines of text in the same place where you set tv.setText ().

 int maxTextViewWidth="ENTER MAX WIDTH HERE"; tv.setText("hello\nhow are you?"); int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxTextViewWidth, View.MeasureSpec.AT_MOST); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); tv.measure(widthMeasureSpec, heightMeasureSpec); int lineCount=tv.getLineCount(); 
+1
source share

All Articles