9

i want to know how many line in my text view. i already set mytextview text, then i want to get how many line it take in mytextview.

i use mytextview.getLineCount() but it didn't work. it always return 0.

can someone helpme.

Alfa
  • 150
  • 1
  • 2
  • 10

6 Answers6

31

You need to post method for fetching the lines counts. Here is the sample code

imageCaption.setText("Text Here");
imageCaption.post(new Runnable() {

    @Override
    public void run() {

        int lineCount    = imageCaption.getLineCount();

        Log.v("LINE_NUMBERS", lineCount+"");
    }
});
Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
  • 2
    getLineCount will still return 0 in this scenario, at least when it's inside of a ListView. –  May 12 '14 at 15:55
2

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;
}
Rajesh M
  • 191
  • 1
  • 3
1

public int getLineCount ()

Since: API Level 1

Return the number of lines of text, or returns 0 if the internal Layout has not been built.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
1

Please check this link.

Android Edittext: Get LineCount in an Activity's onCreate()

tv.getLineCount(), will always retirn 0, if internal layout is not created yet.

Community
  • 1
  • 1
Kamal
  • 1,415
  • 13
  • 24
1

The Following will provide the lines count of the textview at the same place 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();
galhe2
  • 165
  • 1
  • 4
  • 10
  • Please [edit] with more information. I know you've seen others, but code-only and "try this" answers are [discouraged](http://meta.stackexchange.com/questions/196187), because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – Mogsdad Aug 26 '15 at 20:17
-9

thanks for all. i solved it already. i use thread to get the linecount just like this

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

    }
    countLine = textView.getLineCount(); 

}

hope this will help if you have same problem. best regard.

Alfa
  • 150
  • 1
  • 2
  • 10
  • 8
    This code is dangerous if line count "IS" 0. The thread will loop forever. – jclova Jan 16 '13 at 03:20
  • 1
    this will do the trick for above problem--> replace `while loop` with `while(textView.getLineCount() == 0 && ! textView.getText().toString().isEmpty());` – mrsus Jun 20 '13 at 08:16