Android layouts for different screen sizes - android

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.

Related

android phone and tablet layout

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

How can I fix android tablet's width and height

I am making android application in tablet using fragments so that I arranged 8 buttons vertically in 10.1 tablet it is showing fine but in 7.1 tablet only 5 icons it is showing.
So How can I manage height and width properly in all tablets ... please help
Remember to write different xml layouts (with same name) and place to different folders: layout-large, layout-normal, layout-small and layout-xlarge. Same is with drawables (different size images) and values (different type padding and other values). These folders are for different phone/tablet types with various resolution and screen size. If you do it, then you can easily manage layout for small device and not touch big screen devices. Device is picking xml file automatically so it's good practice to place xml files in different folders for various phones.
More information you can find in http://developer.android.com/guide/practices/screens_support.html
You can use different layout for different screen sizes. You can also add different buttons and other drawable also. Please see the link
Supporting Multiple Screens
You can write different xml layouts using different folders:
For example:
res/layout
res/layout-sw600dp //used by 7''
res/layout-sw720dp //used by 10''
Also you can use the same layout with the same component but with different dimensions.
You can use #dimen/my_dimen notation, and then put your my_dimen in different folders like:
res/values/dimens.xml
res/values-sw600dp/dimens.xml
and so on..
Official doc: http://developer.android.com/guide/practices/screens_support.html

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.

Providing layout resources for specific screen sizes

In my Eclipse project I currently have the following folders:
layout-land
layout-large-land
layout-sw600dp-land
layout-xhdpi-land
(I have others too, but the problem right now is for landscape layouts only.)
My layout is quite specific: it varies greatly when I change the size of the screen in Graphical Layout in Eclipse.
From what I have learned from Android docs and from AVD setup, normal screens are from 3.6" to 4.9". Large are from 5.0" to 7.4". Small are up to 3.5".
I go to my xml layout file in the layout-land folder. The screen size chosen already is 4.0", which is all good, because we're in the landscape layout folder for normal screen sizes. I now choose a screen size of 5.1". The editor opens the xml file in the layout-large-land folder -- also OK.
I now choose this screen: 4.65" (720x1280 xhdpi). The size of 4.65" is still in the normal range, and the editor uses the xml file in the layout-land folder. This should be ok, except the fact that my layout changes greatly! I thought that maybe this is because of xhdpi, and that's why I created the folder above (number 4), but that does not seem to work with Eclipse (the xml file is not used for this screen). Same problem occurs with 4.7".
So what my question really is:
How can I provide different xml layouts for screens in the normal range?
Note that there Android platform doesnt consider the screen sizes as much as the dp or Density Pixels. Also there are several of known issues with Eclipse and ADB Plugin for eclipse. The best way to test is with the actual device. For the information to support the screens, we just have to place the files in folders below and it will possibly work for most of the devices with the specified DP.
layout-land
layout-large-land
layout-sw600dp-land
layout-xhdpi-land
http://developer.android.com/guide/practices/screens_support.html

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