2

I've main.xml in layout and layout-xlarge folder. In phones with wider screens like Galaxy Note, I would like to use main.xml from layout-xlarge folder. How can I redirect the android system to use that specific layout file dynamically from code based on condition?

For eg : if some condition is true, then use main.xml file from layout folder and if false use main.xml from layout-xlarge folder.

4
  • Are you using 2 main.xml files, one in each folder?
    – Amt87
    Aug 28, 2012 at 12:09
  • yes Amt87. One in layout and other in layout-xlarge and both have different design. Aug 28, 2012 at 12:23
  • Based on what condition? Aug 28, 2012 at 12:26
  • I need this exact same functionality - for me, qHD screens always fall under HDPI but my XHDPI layouts look perfect on qHD. It would be awesome to custom specify the layout folder to use application-wide when I detect a qHD screen.
    – Kevin
    May 29, 2013 at 23:05

3 Answers 3

2

You can't do that any how you don't want to redirect the Android system if you are using XHDPI devices it will take from layout-xhdpi.. Say for example Samsung S3 it will takes layout as layout-xhdpi.. It always depends on Devices Densities.

Say if DPI is 120dpi - layout-ldpi

DPI is 160dpi - layout-mdpi

DPI is 240 & 256dpi - layout-hdpi

DPI is >256dpi - layout-xhdpi

All above given values is approximately.

1
  • It doesn't only depend on the densities, there are a lot of resource qualifiers other than density at the link Teovald posted below. Screen size, smallest-width, longest-width, even language. Aug 28, 2012 at 12:27
1

Quick solution :

  1. Duplicate main.xml and name it something like main_phone_large.xml

  2. Then in your main activity.

    //whatever width you decide for 
    if(getResources().getDisplayMetrics().widthPixels > 500){
    setContentView(R.layout.main_phone_large.xml);
    }else
    setContentView(R.layout.main.xml);
    
1
  • I agree that is an option, but in my case I've many layout files with the same situation, so creating a copy with new name seems not to be good option and also if that's the case there is direct option for the same by using smallestWidth layout folder (layout-sw) as mentioned in : developer.android.com/guide/topics/resources/…. So please provide me a method to choose it dynamically overriding the layout choosing by android system. Aug 28, 2012 at 12:20
1

You don't have anything to do, as long as you provide different layouts for the different densities, the system will automatically select the one that is adapted to the device screen. You also don't have to provide layouts for all densities. If some elements don't need a different layout, simply create a xml in the layout folder and it will be used for all screen densities.

I highly recommend you read this page of the official documentation : http://developer.android.com/guide/practices/screens_support.html

3
  • Thanks for your answer, I've already went through that article which I posted as comment for above answer. What I need is how to redirect the android system dynamically from code based on my condition. Aug 28, 2012 at 12:25
  • Please, provide with example of conditions you want
    – Amt87
    Aug 28, 2012 at 13:27
  • Unless you have to deal with specific hardware features, you don't need to change your code to manage the different screen densities, the framework already does this by itself. You should NOT have to override the choice made by the framework, unless for example you need to use another layout for a very specific phone (for example a Kyocera echo that has 2 screens).
    – Teovald
    Aug 29, 2012 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.