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

Asked

Viewed 2,093 times

1

How to detect a 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 phone, and I need that according to the type of screen, the layout is applied. Have some sample code to show me ?

  • Related: http://answall.com/questions/28113/como-saber-se-%C3%A9-a-tablet-or-a-smartphone

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

normal-layout 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 implicitly for you and determine which one to use at runtime. No need to worry about java code.

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

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

3

Very simple, 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 go to the head together"

  • Thank you, just one more thing, here is giving for example on the tabhost, a null value, the code to detect the resolution I put inside the oncreate. Need to do something beyond that ? And other, the folder is not being recognized as the one I created, just shows the same default LAYOUT : / .

  • Thank you very much. I managed to solve =D .

  • Mark +1 if I helped you :p

Browser other questions tagged

You are not signed in. Login or sign up in order to post.