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()
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.
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.
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();
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.
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+"");
}
});
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;
}