how to use getlinecount() in textview android

前端 未结 6 1732
忘掉有多难
忘掉有多难 2021-01-12 02:03

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

相关标签:
6条回答
  • 2021-01-12 02:28

    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.

    0 讨论(0)
  • 2021-01-12 02:33

    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.

    0 讨论(0)
  • 2021-01-12 02:43

    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();
    
    0 讨论(0)
  • 2021-01-12 02:45

    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.

    0 讨论(0)
  • 2021-01-12 02:51

    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+"");
        }
    });
    
    0 讨论(0)
  • 2021-01-12 02:53

    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;
    }
    
    0 讨论(0)
提交回复
热议问题