0

Hi i want to fit my app to all screen sizes and to do so i need to get the screen width and height. but if i use this code for example

 DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
int height = displaymetrics.heightPixels; 
int width = displaymetrics.widthPixels

It gives me the pixels so there can happen a situation that it gives me the same dimentions for tablet and a smaller phone. how can i get the actual screen size?

and another thing, i have a game i made with bitmaps and on my phone it is working fine but on tablet the bitmaps are too small how can i resize them according to screen size?

Lokesh
  • 5,180
  • 4
  • 27
  • 42
user3419789
  • 27
  • 1
  • 8

5 Answers5

1

You need screen density and pixel size. (Number of pixels) / (dots per inch) gives screen size in inches.

See: getting the screen density programmatically in android?

DisplayMetrics displaymetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
int height = displaymetrics.heightPixels; 
int width = displaymetrics.widthPixels
int realWidth = (int)((float)width/metrics.density);
int realHeight = (int((float)height/metrics.density);

Answer to the second question depends on how are you loading your bitmaps. You should provide multiple sizes for different devices. Then you can use built-in scaling mechanism - simply place bitmaps in their matching drawable-(dpi)-(screen size) folders. Other way you would have to load images from assets folder and scale them if necessary.

Community
  • 1
  • 1
Zielony
  • 16,239
  • 6
  • 34
  • 39
1

For your 2nd problem of bitmap appearing too small on tablet, try to nine patch your image and then place those images in their respective folders (hdpi, mdpi, xhdpi, xxhdpi) Make sure you use the high quality resolution image for nine patching otherwise your image will get stretch (poor quality). You can nine patch your image from here also nine patching image

Ajinkya S
  • 570
  • 7
  • 17
  • I tried to do that but it is making my images smaller.. and there us a problem if mdpi is smaller image than hdpi that what if there is a tablet with mdpi and a small phone with hdpi? the sizes wont fit – user3419789 Apr 30 '14 at 13:54
  • If your original image is small then obviously your image will appear small, and another thing is try assigning weight for that image. – Ajinkya S May 02 '14 at 04:47
-1

try this:

    Display display = activity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
-1

You can get screen dimensions with this code:

public int getScreenHeight() {
     return getDisplay().getHeight();
  }
private Display getDisplay() {
     return ((WindowManager) getContext().getSystemService(
        Context.WINDOW_SERVICE)).getDefaultDisplay();
  }
public int getScreenWidth() {
     return getDisplay().getWidth();
 }
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36
-1

Try this.

Add this to onCreate() Method. Declare point!

WindowManager wm = ((WindowManager) getSystemService(WINDOW_SERVICE));
    Display display = wm.getDefaultDisplay();
    point = getDisplaySize(display);

//here is the method to get device size.

@Deprecated
@SuppressLint("NewApi")
private static Point getDisplaySize(final Display display) {
    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}
sandy
  • 3,311
  • 4
  • 36
  • 47