¿Cómo podemos obligar al sistema Android a tomar un diseño específico de una carpeta de diseño específica?

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.

preguntado el 28 de agosto de 12 a las 12:08

Are you using 2 main.xml files, one in each folder? -

yes Amt87. One in layout and other in layout-xlarge and both have different design. -

¿En base a qué condición? -

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. -

3 Respuestas

No puedes do that any how you don't want to redirect the Android system if you are using XHDPI devices it will take from diseño-xhdpi.. Say for example Samsung S3 it will takes layout as diseño-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.

Respondido 28 ago 12, 13:08

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. - kevin coppock

Solución rápida:

  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);
    

Respondido 28 ago 12, 13:08

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 : desarrollador.android.com/guide/topics/resources/…. So please provide me a method to choose it dynamically overriding the layout choosing by android system. - Eldhose M Babu

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

Respondido 28 ago 12, 13:08

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. - Eldhose M Babu

Please, provide with example of conditions you want - Amtxnumx

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). - Teovaldo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.