0

I made ScrollView and inside its LinearLayout I put a TextView, I just want to put string in it until the TextView exceeds the layout. The problem whit my code is that while loop never ends.

public class MainActivity extends Activity {
public static int screenWidth,screenHeight;
public boolean overlap;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main) ;



    ScrollView scroll=(ScrollView) findViewById(R.id.scrollView1);
    TextView mytextview=(TextView) findViewById(R.id.textview1);
    TextView textshow=(TextView) findViewById(R.id.textView2);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);

    mytextview.setText("");

    ViewTreeObserver vto=scroll.getViewTreeObserver();
    getmeasure(vto,mytextview,scroll,linearLayout);
}



public void getmeasure(ViewTreeObserver vto, final TextView mytextview2, final ScrollView scroll2, final LinearLayout linearLayout2) {


    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int a=linearLayout2.getMeasuredHeight();
            int b=scroll2.getHeight();

            while (a<b) {
                mytextview2.append("full    full    full");
                a=linearLayout2.getMeasuredHeight();
                b=scroll2.getHeight();
                }

            }
    });

}
mmBs
  • 8,421
  • 6
  • 38
  • 46
Soroush Aghaiee
  • 193
  • 1
  • 1
  • 4

1 Answers1

0

The method getMeasuredHeight() returns the heigth, that has been measured in onMeasure(). The Problem with your code is, that getMeasuredHeight() does not change because onMeasure() has not been called by the Android Framework. Actually your while loop prevents the Framework from measuring the Views.

Implement the OnGlobalLayoutListener like this:

vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        int a=linearLayout2.getMeasuredHeight();
        int b=scroll2.getHeight();

        if (a<b) {
            mytextview2.append("full    full    full");       
        }

   }
});

When the text is appended after the layouting the LinearLayout and its parent (ScrollView) should get invalidated and thus the views will be layouted again. Layouting includes measuring the Views. That means your OnGlobalLayoutListener will be called again.

Be advised that this is not a good method to fill a screen with text. Actually you don't need a ScrollView to make a TextView scrollable vertically. And why do you even need a ScrollView if you do not want its contents to be higher than the screen?

thaussma
  • 9,756
  • 5
  • 44
  • 46
  • "The Problem with your code is, that getMeasuredHeight() does not change because onMeasure() has not been called by the Android Framework."thanks for your answer. “Be advised that this is not a good method to fill a screen with text”so please advise me a way ,I want put a very long text into separated textView s, and each textView takes whole screen. (each textView is in a pageView,I just want to know when break text to fit screen) Thanks. – Soroush Aghaiee May 22 '13 at 09:53
  • There is no easy solution I know to that problem. But you should not use the Frameworks layouting process again and again to resolve the right amount of text. You could measure the text like in the answer of http://stackoverflow.com/questions/14276853/how-to-measure-textview-height-based-on-device-width-and-font-size and search for the correct text amount. Or use TextView.getOffsetForPosition() to determine the last character in the TextView and start with the following character at the next page. – thaussma May 22 '13 at 10:15