8

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)?

martin k.
  • 156
  • 3
  • 15
  • Please check this thread for a correct solution: http://stackoverflow.com/questions/3355367/height-of-statusbar – fpanizza Jun 03 '14 at 14:49

3 Answers3

6

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);
    }
}
CodeMonkey
  • 1,136
  • 16
  • 31
3

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.

Reaper
  • 486
  • 3
  • 12
0

you can use also the full screen

developer.android

wSakly
  • 387
  • 1
  • 10
  • 1
    thanks a lot... but anyway, isn't there another method? Because this could be done only for the 'KitKat' devices – martin k. Jun 03 '14 at 14:35
  • i use this method : context.resources.displayMetrics.widthPixels context.resources.displayMetrics.heightPixels – wSakly Jul 13 '18 at 12:50