Reputation: 148
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.
Upvotes: 9
Views: 15394
Reputation: 165
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();
Upvotes: 1
Reputation: 291
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;
}
Upvotes: 2
Reputation: 4835
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+"");
}
});
Upvotes: 31
Reputation: 148
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.
Upvotes: -10
Reputation: 1415
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.
Upvotes: 1
Reputation: 27549
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.
Upvotes: 1