1

I have a simple app that works well for phones, with the following structure:
enter image description here

Now, I want to take better advantage of the unused space when the user is
using the app in a tablet or in landscape mode. Pretty much like this:

enter image description here

Is there any way I can make a check (resolution or device type) that I can
switch the xml layout from phone_layout.xml to tablet_layout.xml on onCreate?

OR I should be using fragments for this?
Thanks.

inrob
  • 4,969
  • 11
  • 38
  • 51

3 Answers3

1

You can make use of the resource buckets.

Basically you will have to create two layout files with the same name in two different resource folders:

  • res/layout/your_layout.xml: this will be your default layout for any other devices than the below mentioned
  • res/layout-sw600dp/your_layout.xml: this layout will be picked by devices with the smallest width of 600 dp (tablets fall into this category)

The system is intelligent enough to pick the right layout based on the running device as long as you name your layouts the same.

Andrei Catinean
  • 863
  • 10
  • 15
1

Referring to Android documentation : http://developer.android.com/training/basics/supporting-devices/screens.html

you can set multiple layout using the same name but under different folder that indicates the screen resolution (it could be used for other configurations like: language, screen orientation, and dp)

MyProject/
   res/
       layout/
           main.xml
       layout-large/
           main.xml

P.S. this methodology can be used for drawables

MyProject/
   res/
       drawable-xhdpi/
           awesomeimage.png
       drawable-hdpi/
           awesomeimage.png
       drawable-mdpi/
           awesomeimage.png
       drawable-ldpi/
           awesomeimage.png
tahhan
  • 81
  • 6
-1

For the tablet create a folder in /res/layout-sw600dp/activity_layout.xml for vertical viewing, and a folder in /res/layout-sw600dp-land/activity_layout.xml for horizontal viewing.

For the phone, in addition to the normal folder in /res/layout/activity_layout.xml, add another folder /res/layout-land/ for horizontal viewing.

For the respective images in activity_layout.xml created folders /drawables- sw600dp, /drawable-sw600dp-land for landscape display.

https://github.com/ciromelody/MyTabletPhone

remix23
  • 2,632
  • 2
  • 11
  • 21