Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

content view width and height

I am using below steps to know the width and height of contentview on my display screen

ContentViewWidth = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getWidth();
ContentViewHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();

but this method doesn't work inside onCreate or onResume and if I am using these steps inside onWindowFocusChanged I get force close error on my phone

Please help me how can I get the content view of my display screen. Content view is excluding statusbar and titlebar dimensions

like image 749
prateeksarda Avatar asked Nov 23 '25 01:11

prateeksarda


1 Answers

I use the following technique - post a runnable from onCreate() that will be executed when the view has been created:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

This code will run on the main UI thread, after onCreate() has finished.


If you are trying to get the height of the content view itself, you need to subtract the padding from the height - this is because Android 2.x lays out the views differently to 4.x.

contentHeight = contentView.getHeight() - contentView.getTopPadding();
like image 131
Richard Le Mesurier Avatar answered Nov 26 '25 12:11

Richard Le Mesurier