Search code examples
androidandroid-layoutscreen-resolution

Separate 1024 width devices


I am developing an Android app that is causing me problems with devices that have 1024 as their width dimension. I have tried creating a layout folder layout-w1024 and layout-mdpi-sw1024dp; but the device uses the layout-mdpi folder instead. I am using 15 as the minimum android version.

Is there any other way using which I can separate 1024 devices?


Solution

  • You can do it programmatically if it's suitable for your project, something like this would call a different layout depending on the dimensions of the screen. This has some limitations though since it can only be called from an Activity (shouldn't be a problem in your case) and if you have problems with layouts all over your application it may not be very scalable.

    The conditional width == 1024 should probably be replaced with something greater than or more specific, you'll need to play with this for a while to make it work. You should also account for the height of the screen, not only the width.

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

    Or if you want it in pixels instead:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    

    And then:

    //Depending on the width, set one layout or anotherone
    setContentView(width == 1024?R.layout.for1024:R.layout.normal);
    

    Source