Android - Height of title bar

446 views Asked by At

I want to get the title bar height (not the status bar)

I need this in order to know the real height the app can use.

I saw on SO some code and I tried it and it returned height=0.

So, I did some more search and I found that I have to put it inside a runnable and call it with a post-delayed.

Here is the code:

    Handler mHandler = new Handler();

    Runnable r = new Runnable() {  
        @Override  
        public void run() {  
            Rect frame = new Rect();  
            getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
            int titleHeight = frame.top;  
            Log.d(TAG, titleHeight); 
        }  
    };  

    mHandler.postDelayed (r, 200);

This is working. The Log.D shows the correct height, But now there is a new problem. I cannot use the titleHeight variable for further programming inside my onCreate.

What to do?

Thanks, AJ

1

There are 1 answers

6
Booger On

The scope of that variable is inside the run method. If you want to reference that variable, you should change it to a larger scope (define it outside of this method).