immp8q
Last Updated: June 20, 2018
·
13.19K
· ignaciogs
Bc0b374c428b10843153778f8542784c

Get height of a view in onCreate method (Android)

final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
final ViewTreeObserver observer= layout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
            public void onGlobalLayout() {
                Log.d("Log", "Height: " + layout.getHeight());
            }
        });
Say Thanks
Respond

1 Response
Add your response

30034

You can use a layout or pre-draw listener for this, depending on your goals. For example, in onCreate():

final View content = findViewById(android.R.id.content);
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//Remove it here unless you want to get this callback for EVERY
//layout pass, which can get you into infinite loops if you ever
//modify the layout from within this method.
content.getViewTreeObserver().removeGlobalOnLayoutListener(this);

    //Now you can get the width and height from content
}

});
best regards
https://downloadnox.com/ https://showbox.software/ https://vidmate.onl/

about 1 hour ago ·