android phone and tablet layout - android

I have a simple app that works well for phones, with the following structure:
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:
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.

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.

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

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

Related

dimension folder for different screen sizes [duplicate]

I am creating different values folders in my app (values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi, values-nodpi, values-w360dp-mdpi). But some devices that belong same category. But having different screen sizes. But I see give font size according to device densities in this the answer provided by #PankajSharma suggest to create folders like-
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
I want to know what is the difference b/w my way and the other way? I think the answer provided by #PankajSharma is easy. I also want to know which way is better?
The approach you are using is a valid approach, but a little outdated. From HoneyComb, there is a new way to fix all of this. Your resources folder should now look like this:
Please refer to the link I have posted and familiarize yourself with Smallest Width concept.
Hope this helps :)
EDIT: Adding to this post, try to establish some kind of standardization in your dimens.xml, something like this:
Doing this makes it easier to maintain code, plus it reduces the number of dimen folders. Normally rather than having values-hdpi, values-xhdpi, etc. files like values-sw480dp-xhdpi might have more values to adjust, but then again all of this is contextual.
Create a Single layout for default screens 4.7 inch (hdpi) in layout folder and dimensions in values folder. This is your Superset.
Now let say you want your layouts for 7inch devices. Create values-sw600dp folder for 7inch in Portrait orientation
Now lets say you want your layouts for 10 inch devices Create values-dw720dp folder
NOTE :- For landscape just add "-land" in front of folder names.
Now lets say you have new devices such as Xperia SP (4.7' and XHDPI) and Nexus 5(5" and XXHDPI).
For these, you can create values-xhdpi and values-xxhdpi folders and similary add -land for landscape orientation..
I hope you got the point of how to create folders..
Now your superset is defined in values folder. Most of the dimensions will be used from here only. Now run your app in other devices. Whatever mismatch is occuring just add that specific dimension in their respective values folder
To check from which folder your layouts, images are used, use my trick.
Create five same strings and put in it all the values folders like this :-
Default Screen
Screen 4.7
XHDPI Screen
MDPI Screen
Create five drawable folders, most of them will be already there : - drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
Put the screenshots below in their respective folder under the same name
This is how my res folder looks like and i am supporting all the devices from 4.7 screen and above :-

Android layouts for different screen sizes

I am struggling for layouts for an Android app. I have defined different layouts for different screen sizes and the current layout directory structure is something like this:
layout
layout-land
layout-small
layout-xlarge
layout-xlarge-land
Problem
The main layout directory files are being displayed for 3.7 to 7.0 which is a very broad range. And text overlap on small screens and if I adjust on small; it becomes very tiny on the large screen.
According to me, the layout small files should be rendered for smaller screens but those files are only rendered for android wear devices.
layout-xlarge seems to work for Nexus 9 to Nexus 10
I want to know, how can I define the different layout for 3.7-5.0 and 5.0-7.0 screen sizes.
The answer is already given by user5594218 but looks like you are still unclear. (maybe you are beginner)
So, here is the step by step guideline
Solution 1: (short and simple)
Navigate to app > src > main > res
Duplicate layout directory by copying and pasting
Rename duplicated Directories e.g layout-sw300dp
Solution 2: (bit lengthy)
Create new resource directory: res > New > Android resource directory
Select Resource Type as layout
Add sw<N>dp in Directory name e.g layout-sw300dp and Hit OK
Navigate to app > src > main > res
Copy layouts XML file inside new directory
//repeat process for other qualifiers
List of Qualifiers to support all screens:
layout-sw300dp
layout-sw330dp
layout-sw480dp
layout-sw600dp
layout-sw720dp
Testing:
This is how it gonna look like, if you did it right.
For more detail and examples, check: Android Application Development All-in-One For Dummies
Create folders like the following
layout-sw300dp
layout-sw330dp
layout-sw480dp
layout-sw600dp
layout-sw720dp
Create also values-folders like
values-sw300dp
values-sw330dp
values-sw480dp
values-sw600dp
values-sw720dp
Navigate app-> res-> layout and right click then New-> layout resource file write the filename correctly and from available qualifiers choose the size you want.
Dear you don't need to declare layout for various screen. just make single layout for every screen and don't fix the width and height of layout. you have to keep height either match parent and wrap content as well as for width.
you should try dp in your layout-height/layout-width/textsize as it is independent of screen sizes.

Different values folders in android

I am creating different values folders in my app (values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi, values-nodpi, values-w360dp-mdpi). But some devices that belong same category. But having different screen sizes. But I see give font size according to device densities in this the answer provided by #PankajSharma suggest to create folders like-
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
I want to know what is the difference b/w my way and the other way? I think the answer provided by #PankajSharma is easy. I also want to know which way is better?
The approach you are using is a valid approach, but a little outdated. From HoneyComb, there is a new way to fix all of this. Your resources folder should now look like this:
Please refer to the link I have posted and familiarize yourself with Smallest Width concept.
Hope this helps :)
EDIT: Adding to this post, try to establish some kind of standardization in your dimens.xml, something like this:
Doing this makes it easier to maintain code, plus it reduces the number of dimen folders. Normally rather than having values-hdpi, values-xhdpi, etc. files like values-sw480dp-xhdpi might have more values to adjust, but then again all of this is contextual.
Create a Single layout for default screens 4.7 inch (hdpi) in layout folder and dimensions in values folder. This is your Superset.
Now let say you want your layouts for 7inch devices. Create values-sw600dp folder for 7inch in Portrait orientation
Now lets say you want your layouts for 10 inch devices Create values-dw720dp folder
NOTE :- For landscape just add "-land" in front of folder names.
Now lets say you have new devices such as Xperia SP (4.7' and XHDPI) and Nexus 5(5" and XXHDPI).
For these, you can create values-xhdpi and values-xxhdpi folders and similary add -land for landscape orientation..
I hope you got the point of how to create folders..
Now your superset is defined in values folder. Most of the dimensions will be used from here only. Now run your app in other devices. Whatever mismatch is occuring just add that specific dimension in their respective values folder
To check from which folder your layouts, images are used, use my trick.
Create five same strings and put in it all the values folders like this :-
Default Screen
Screen 4.7
XHDPI Screen
MDPI Screen
Create five drawable folders, most of them will be already there : - drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
Put the screenshots below in their respective folder under the same name
This is how my res folder looks like and i am supporting all the devices from 4.7 screen and above :-

Android scale image for XXHDPI

How can I make 2 separate layout folder for
screens that are xxhdpi, and screens that are everything else?
Thanks!
I have right now just a layout folder for all my layouts
You can have folders under /res that are named as follows (where "xxhdpi" refers to the Density):
layout
layout-xxhdpi
However, you may actually be referring to the Size of the screen itself, which can be:
layout
layout-xlarge
as an example...
Please refer to the Android Providing Resources docs for more information on the flavors of folder names for resources.

How can we force Android System to take a specific layout from a specific layout folder

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.
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.
Quick solution :
Duplicate main.xml and name it something like main_phone_large.xml
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);
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

Categories

Resources