immp8q
Last Updated: October 25, 2018
·
17.47K
· ignaciogs

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

7 Responses
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/

6 months ago ·
30108

package com.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

public class AnimateScreen extends Activity {

//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout ll = new LinearLayout(this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAPCONTENT, LinearLayout.LayoutParams.WRAPCONTENT);
layoutParams.setMargins(30, 20, 30, 0);

Button bt = new Button(this);
bt.setText(String.valueOf(bt.getWidth()));

RotateAnimation ra = new RotateAnimation(0,360,bt.getWidth() / 2,bt.getHeight() / 2);
ra.setDuration(3000L);
ra.setRepeatMode(Animation.RESTART);
ra.setRepeatCount(Animation.INFINITE);
ra.setInterpolator(new LinearInterpolator());

bt.startAnimation(ra);

ll.addView(bt,layoutParams);

setContentView(ll);

best regards

https://youtubego.me/ https://black-mart.net/

5 months ago ·
30169

thank you for sharing <a href="http://freemoneytransferapps.xyz/">free money transfer apps</a>

5 months ago ·
30457

Just download the latest PES 2019 APK and OBB files from here http://pes2019download.com/

3 months ago ·
30561
30894

Happy Thanksgiving to our wonderful InFormation Each and every one of you inspire us every day Thanks For Posting
https://tutuappapk.vip https://tutuhelperx.com https://emus4u.co https://tutuappk.com

about 2 months ago ·
30973

I'm looking for a good way to measure the dimensions of the actual content area for an activity in Android.
https://www.playbuzz.com/voippe10

about 2 months ago ·