How to detect the screen resolution to apply the layout in android?

1

How do I detect a particular screen size and apply the specific layout? Example:

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

    
asked by anonymous 10.12.2014 / 06:07

2 answers

4

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

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

As you will do a screen for each size I suggest you study a little about Fragment because you will probably need it.

link

    
10.12.2014 / 14:53
2

Soooo simple, my friend. Use this 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 get together"

    
10.12.2014 / 12:06