0

I'm testing my app on two very different handsets:

Htc One X (running 4.1)
720x1280, density XHigh (320dpi)

Samnsung GT-S6500D running 2.3.6
320x480, density medium, (160dpi)

Now, How can I make sure that each device use different layouts folder? I have created layout-sw720dp, layout-sw600dp, layout-normal, ...

Because 2.3 doesn't take layout-sw320dp into account, I guessed it would take layout-normal, wich it does, good.

Now why doesn't the bigger screen take layout-sw720dp folder? It also falls into layout-normal >> why?

I use

    android:minSdkVersion="10"
    android:targetSdkVersion="17" 

and build target = level 15 (4.0.3)

Arun C
  • 9,035
  • 2
  • 28
  • 42
Greg
  • 689
  • 2
  • 8
  • 23

2 Answers2

2

get screen resolution and divert it accordingly

    private final float LOW_LEVEL=0.75f;
    private final float MEDIUM_LEVEL=1.0f;
    private final float HIGH_LEVEL=1.5f;

    float level = getApplicationContext().getResources().getDisplayMetrics().density;
            if(level == LOW_LEVEL){
                    setContentView(R.layout.activity_login);
            }else if(level == MEDIUM_LEVEL){
                    //do smoothing here
                    setContentView(R.layout.activity_login);

            }else if(level == HIGH_LEVEL){
                    //do something here
                    setContentView(R.layout.activity_login);
            }else {
                    //do something here
                    setContentView(R.layout.activity_logins);
            }
sourabh kasliwal
  • 947
  • 3
  • 8
  • 21
  • My app is quite complex in terms of number of activites and fragments, if I had to go for this method, I would need to update A LOT of classes. Is that the only way to do it? According to the documentation, layouts folders are meant to handle different screen sizes. – Greg May 29 '13 at 09:53
  • for that try this http://developer.android.com/guide/practices/screens_support.html – sourabh kasliwal May 29 '13 at 09:56
0

I think the problem is you are using two different layout classifications. Normal is used as generalized screen density why the layouts available since 3.2 are based on minimum screen dimensions. Try removing normal density layout and see what happens. Of course you can always inflate different layouts from code as above answer suggested.

Igor Čordaš
  • 5,785
  • 4
  • 42
  • 54
  • Thanks, but I tried that, it crashes when running on 2.3 because (I suppose) it doens't find any layout folder (only layout-swdp) which is not supported if phone version < 3.2 – Greg May 29 '13 at 09:56
  • I missed the part that you were targeting older versions too. Other possible problem could be that Htc One X doesn't actualy have 720 px but a little less so does't fit in that category since the number means minimum. This affects Nexus phones for example because the on screen buttons reduce screen size available for the app. – Igor Čordaš May 29 '13 at 11:21
  • Yes but Log.d(LOG_TAG, "wXh = "+metrics.widthPixels+"x"+metrics.heightPixels); does log me 720x1280 , and if it wasn't the case, it should fall into layout-sw600dp at least. But it doesn't. I'm implementing sourabh kasliwal proposition, which is not a good solution for me but at least it does work. – Greg May 29 '13 at 11:30
  • 1
    Also there is a discussion similar to this on http://stackoverflow.com/questions/9006494/how-do-i-target-1280x720-wxga720-resolution-like-new-galaxy-nexus-in-android-l so check there answers as they might help. Check the last answer by @Tary since I think that is what could solve your problem. Basically he says normal overrides specific resolution displays and you should use normal-1280x720 folder. – Igor Čordaš May 29 '13 at 11:32
  • Thank you, indeed, creating a folder layout-normal-1280x720 works. I'm confused. I don't want to create a folder for each screen resolution existing on this planet. :) – Greg May 29 '13 at 12:14
  • You're welcome. Keep an open mind for auto-scaling of content that Android does and that downscaling is usually quite good so you only need one drawable folder with good 9patch images. On the layout part yes it's quite annoying coping same or similar files just to get an effect but I think there is a way to reference same layout from different folders so you have only one xml. – Igor Čordaš May 29 '13 at 12:26
  • Please mark question as answered if mine or @estoke answer solved your problem. – Igor Čordaš May 30 '13 at 09:25