1

I'm trying to get statusBar height inside onCreate but the methods that I found here need some view already draw to get it's size and then calculate statusBar height.

Since I'm on onCreate, there's nothing draw yet for me to get it's size

Someone can help me here?

2 Answers 2

2
root = (ViewGroup)findViewById(R.id.root); root.post(new Runnable() { public void run(){ Rect rect = new Rect(); Window win = getWindow(); win.getDecorView().getWindowVisibleDisplayFrame(rect); int statusHeight = rect.top; int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleHeight = contentViewTop - statusHeight; Log.e("dimen", "title = " + titleHeight + " status bar = " + statusHeight); } }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Use a global layout listener

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null); // set a global layout listener which will be called when the layout pass is completed and the view is drawn mainLayout.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { // measure your views here } } ); setContentView(mainLayout); 

[EDIT]

To do this only once:

ViewTreeObserver observer = mainLayout.getViewTreeObserver(); observer.addOnGlobalLayoutListener (new OnGlobalLayoutListener () { @Override public void onGlobalLayout() { // measure your views here mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); 

2 Comments

is there any other way where i can call the measure method only one time (on app start)
Set the listener as I have it above, then remove it in onGlobalLayout()! Please try to get into the habit of reading the Android documentation. developer.android.com/reference/android/view/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.