How to detect screen resolution to apply layout on android?

How to detect certain screen size and apply the specific layout ? Example:

I created a layout folder called "layout_480x800", in it will be the layouts for this resolution, I also have a layout folder called" layout_720x1280 " in which will suit my other mobile, and I need that according to the type of screen, the layout is applied. Do you have any sample code to show me ?

Author: ramaral, 2014-12-10

2 answers

Hello, you should use folders with this nomenclature.

Layout-XLARGE screens of at least 960dp x 720dp

Layout-large screens of at least 640dp x 480dp

Layout-normal screens of at least 470dp x 320dp

Layout-small screens of at least 426dp x 320dp

Inside these folders you create the layout(xml) files with the same name. The system will implicitly check for you and determine which use at runtime. No need to worry about java code.

As you will make a screen for each size I suggest you study a bit about Fragment as you will probably need it.

Http://developer.android.com/guide/components/fragments.html

 4
Author: Skywalker, 2020-06-11 14:45:34

Very simple, mate. Use it and develop!

Configuration config = getResources().getConfiguration();

                if (config.smallestScreenWidthDp >= 480) 
                {
                    //Toast.makeText(this, "Igual ou maior que 480dp", Toast.LENGTH_SHORT).show();
                    setContentView(R.layout.layout_480x800);

                } else if (config.smallestScreenWidthDp == 720) 

                {
                    //Toast.makeText(this, "Igual a 720dp", Toast.LENGTH_SHORT).show();
                    setContentView(R.layout.layout_720x1280);
                }

"let's go forward together"

 2
Author: Lollipop, 2014-12-10 11:06:29