0
votes

I have a app with a textView and a Button in them, when the onCreate is called, I call textView.getHeight() and the value returned is 0. (tried this also in onResume and onPostResume).

when the user clicks on the button, it calls again textView.getHeight() and now the answer I got is 864 instead of 0. why is that so?

(the layout has only a TextView and a Button - the parent is RelativeLayout)

edit: the TextView has in xml : android:layout_height="match_parent" (the parent is the RelativeLayout, the base parent view of the layout), so the true height must be > 0.

the code:

TextView text;
Button button;

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

    text = (AutoScrollingText) findViewById(R.id.text);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);

    Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();

}

@Override
protected void onPostResume() {
    super.onPostResume();

    Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();

}

@Override
public void onClick(View view) {
    switch(view.getId()){
        case (R.id.button) : {
            Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
        }
    }
}
1
in onCreate the View not is draw. Use View.post()Thorny84
can you please explain how to use "View.post() "?Admond

1 Answers

1
votes

Use this

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

            text = (AutoScrollingText) findViewById(R.id.text);
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(this);

            text.post( new Runnuble(){
                Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
             });

        }

It should work