Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get USABLE screen width and height in Android

I'm creating an app where I really need to know the correct screen dimensions. Actually I know how to do that... but a problem appeared, because even if you dorequestWindowFeature(Window.FEATURE_NO_TITLE);, this:enter image description here

still stays on the screen and it seems, it's also counted as a part of the screen so the "screenWidth" is actually bigger, than the usable part.

Isn't there any method how to get the size of the usable part and not whole screen? Or at least get the sizes of the 'scrolling thing'(but there would be a problem, that there are devices that don't show them)?

like image 236
martin k. Avatar asked Jun 03 '14 14:06

martin k.


People also ask

How do I find my screen width and height?

The size of a 16:9 screen depends on how long the screen's diagonal is, as 16:9 is merely the ratio of the screens width to its height. If you have the screens diagonal, you can multiply that measurement by 0.872 to get the screen's width. You can also multiply the diagonal by 0.49 to get the screen's height.

How do I set Android layout to support all screen sizes?

To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of some view components.

How do I find out the size of my screen on my phone?

If you want the display dimensions in pixels you can use this code: Display display = getWindowManager(). getDefaultDisplay(); int width = display. getWidth(); int height = display.


2 Answers

I had the same question a while back and here is the answer that i found. Shows the activity dimensions.

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.Point;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main)

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        int width = size.x;
        int height = size.y;

        //Set two textViews to display the width and height
        //ex: txtWidth.setText("X: " + width);
    }
}
like image 123
CodeMonkey Avatar answered Oct 06 '22 00:10

CodeMonkey


I know this question is really old, but I have fought with this so many times until I found this stupidly simple solution:

public final void updateUsableScreenSize() {
    final View vContent = findViewById(android.R.id.content);
    vContent.post(new Runnable() {
        @Override
        public void run() {
            nMaxScreenWidth = vContent.getWidth();
            nMaxScreenHeight = vContent.getHeight();
        }
    });
}

Run this code after setContentView() in your Activity's onCreate(), and delay any code that depends on those values until onCreate() is finished, for example by using post() again after this one.

Explanation: This code obtains your activity's root view, which can always be accessed by generic resource id android.R.id.content, and asks the UI handler to run that code as the next message after initial layout is done. Your root view will be of the exact size you have available. You cannot run this directly on the onCreate() callback because layout did not happen yet.

like image 28
Reaper Avatar answered Oct 06 '22 02:10

Reaper