Layout Issue on Small Screens - android

I'm working on a Android App. Layout working fine on every device except where device screen size 480x800 & less.
How can create separate layouts by only targeting that screen size or less?
I'm already tried layout-hdpi, layout-small-hdpi & layout-normal-hdpi since phone like Nexus S, Nexus One are in hdpi category. But when I create separate layouts like layout-hdpi those layouts are affecting phone with bigger screen like Pixel, Pixel2, Nexus 5 etc.
Thanks in Advance

Phones (as opposed to tablets) tend to come in about three size groups (as far as Android resources are concerned): those with smallest width of 320dp, those with 360dp, and those with 410dp. The resources framework gives you a way to target any device larger than a certain width, so the correct technique is to put layouts for small screens on the default folder, and layouts for larger screens in one of the qualified folders.
Since it sounds like your layouts currently work well for anything 360dp or larger, you can make two layout directories:
res/
layout/
layout-sw360dp/
Put the special layouts for the small screens inside res/layout/ and put the “normal” layouts in the other directory.

Its not easy but you can also specify layouts based on the smallest width and you you supply the numbers. You can get more detailed and separate layouts that way however in my experience you will still run into that problem from time to time. In my current project using this method I have specific folders for W360 and W400 to deal with smaller devices for one specific screen.
https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

What you are looking for, is a layout bucket with the smallest width qualifier.
A layout with a set smallest width will be active, unless the smaller of the both dimensions (width or height) is lower than a certain value you can choose.
So, when a device has a smallest width below some value X it will use the default layout, when the value is X or more it will use the defined smallest width layout.
To create such a layout file in Android Studio, you wanna right-click your layout directory, click on new -> Layout Resource File and select the Smallest Screen Width qualifier from the list of available qualifiers. Now you need to specify your smallest width, which in your case should be slightly above 480dp. Give it the same name as your current layout file. Place your code for devices with a smallest width above 480p here. Now change the code in your other layout file, without the smallest width qualifier, to support smaller screens.
For further reference take a look at the official Android Developer page.

Related

Is there any way to force Android to choose a particular layout for a particular screen size?

I am a newbie Android developer and having a little problem. I have designed a screen layout for small screens (eg. 2.7" 240 x 320) with screen size qualifier it worked perfectly. Now when I designed layout for normal screens using smallest screen width qualifier. The app is working normal for normal screen phones but for small screen size (say 2.7"inches) the android is skipping the small screen layout and choosing the sw320dp qualifier layout.
So, is there any way so that android must choose small size screen layout (for 2.7"inches) and for normal screen phones it must choose sw320dp qualifier screen layout.
The swXXXdp resource qualifier is known as the smallest width qualifier. Using sw320dp tells the system to use those values for any device with a smallest width of at least 320dp. Therefore, if you're trying to create one layout for phones with exactly 320dp, and another layout for any phone larger than 320dp, you'd need to use a different number in your resource qualifier.
In your case, you could probably solve the problem by changing your directory to be layout-sw321dp.
In my experience, the common cutoffs I used were:
layout for small phones
layout-sw360dp for modern (larger) phones
layout-sw600dp for seven-inch tablets
layout-sw720dp for ten-inch tablets

Android, different layout for 10.1 and 9.7 inch displays

I've developed an app for an specific 10.1 inch display (2560x1600) and my layout files are written to fit that specific resolution.
Now I need to make the same app to work with an 9.7 inch display (2048x1536).
It's the first time I have to do a thing like this and I don't know which is the right approach. Using the actual layout file in the new tablet is a bit of a mess, nothing fits correctly, is like making a zoom in the old one.
I've read a bit about creating folders for different layouts, but the problem is that if I create a folder "layout-sw720dp" both tablets use the same layout, which I don't want to.
Is the "different layout files" the right approach? If so, how can I specify a layout for an specific "resolution" or whatsoever?
Is there other way to approach this?
Thanks!
In Android, there are different types of attribute qualifiers which helps us to design the app for different types/resolutions of devices. However in this particular case here, both the resolutions would come under the same attributes for almost all the qualifier types.
They both would come under the xhdpi bracket for xlarge devices. So you cannot use these qualifiers. But the one thing that is not same will be the shortest width qualifier for the two resolutions.
The shortest width for 2560 x 1600 would be 800dp and shortest width for 2048 x 1536 would be 768dp. So you can use the layout-sw<>dp attribute to distinguish the two devices.
for 10.1 inch display (2560 x 1600) : layout-sw800dp
for 9.7 inch display (2048 x 1536) : layout-sw768dp
When you use the layout-sw720dp folder, both the devices will take the same folder as the shortest width of both devices are greater than 720. Check this developer doc section (especially the table) for more information.
In the app folder of your application in android studio , go to the res-> layout, right click on that select New -> Layout resource file. on the new resource file window ,give file name dimens.
on the available qualifiers select dimension and push it into the chosen qualifier and specify the dimension as 2048x1536. you can use this file to specify the layout for tablet of dimension 2048x1536.
If you layouts are properly designed layout-sw720dp should handle both tablets flawlessly.
how can I specify a layout for an specific "resolution" or whatsoever?
For this part there are 2 types of width prefixes. layout-wXXX is for devices whose width is exactly XXX, nothing more, nothing less. Whereas, layout-swXXX is for devices whose width is at least XXX.
Again, well designed layout-sw720dp folder should cover both your needs.

Android layouts size and resize

I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout

Supporting multiple screen size - Android

I am going to develop new application in Android. This application should only work in portrait (even for tablet). Also the UI and layout design should be similar on phones and tablet. We can't change the layout design for tablet as it has huge area to use. We have to stretch all the images to match phones. We can use nine patch. But I am little bit confused of using images in multiple drawables.
As per my analysis (may be wrong.. : ) ) the screens are divided into density and sizes. We can use the scaling ratio of 3:4:6:8. But this ratio is based on the density. But in my case I have to stretch the entire UI to fill the Tablet screen.
So what are the drawables that can be used for a app like this which can support multiple devices. And what are the screen sizes for which we have to design.
And this application needs nearly 100 layouts. So I am planning to maintain single layout and designing the layout using weight for each layout instead of using dimension.
Also if I used multiple APKs to support different screen size what are the drawables used to support
1. Small and Normal
2. Large
3. Xlarge
I just did something very similar. To stretch the app without creating new layouts I used dimensions set in XML
res/values/dimensions.xml
res/values-sw600dp/dimensions.xml -> 7+ inches
res/values-sw720dp/dimensions.xml -> 10+ inches
Dimensions are resources files:
<dimen name="default_padding">11dp</dimen>
You can increase the dimensions by about 30% in the 600 and 720 file.
Then simply used #dimen/default_padding in your layout and it will be scaled
Regarding images, either you make sure you have all your assets in all densities, or you set fixed size to you ImageView's and appropriate scaleType
Firstly, you do NOT want to create multiple APKs to support multiple screen densities. Android provides all of the framework you need to support everything within one build, you just need to create the right resource hierarchy drawables with your desired densities.
So what exactly do you need... based on your question the following:
portrait mode: you can specify this in each Activity declared in your AndroidManifest file using the following:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation" >
...
</activity>
NOTE: Per the Android docs, if you're targeting API >= 13, and you use the android:configChanges attribute you should also use the android:screenSize attribute to help specify size changes.
dimension sizes for various screens: as touched upon, this can also be handled in resources. This is your best way to use one common layout file but configure the layout for use on numerous devices. See http://developer.android.com/guide/topics/resources/more-resources.html#Dimension for how to use dimensions if you're unfamiliar
drawables: it sounds like this is the crux of your question. As you mentioned, using nine-patches will help you reduce your app footprint and fill in extra space (see here and here for more on nine-patches). The sizes you should support and the densities needed for those sizes are discussed in great detail in Android design docs, so much detail I could not even do it justice rehashing it here. I've provided links below to as many places as I could remember that this is discussed.
Good luck!
Here are links to Android design docs that you will find useful (some of which have been mentioned):
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/design/style/devices-displays.html
In addition to the pixel density specific folders, you can specify screen-size specific folders
drawable/
drawable-large/
drawable-xlarge/
drawable-hdpi/
drawable-large-hdpi/
drawable-xlarge-hdpi/
drawable-xhdpi/
drawable-large-xhdpi/
drawable-xlarge-xhdpi/
So you could design scale appropriate graphics for the various screen sizes and densities. Please note that a give screen size category (e.g. "large") will only give you a rough idea as to the actual device pixel dimensions of the device, but you'll get good guidelines for min/max dp ranges.
For example, you might have a 100x100 image you want to display on phones (screen size "normal"), you'd create image assets at 100x100, 150x150, 200x200 for drawable, drawable-hdpi, drawable-xhdpi folders respectively. But on 7" tablets, i.e. "large" screen size devices, you might display this same image at 200x200, so your "drawable-large" folder assets would be 200x200, 300x300, 400x400, and on 10" tablets, i.e. "xlarge" screens, you might display the same image at 300x300, 450x450, 600x600, so these go in "drawable-xlarge-*" folders.
All the details are here:
http://developer.android.com/guide/practices/screens_support.html
First you need all the possible screen layouts
drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi // phones like s4
drawable-xlarge
drawable-tvdpi // nexus 7 etc
drawable-xlarge-xhdpi //tablet like nexus 10
layout : for phone
layout-sw600dp
layout-sw720dp
then you need to put all use 9- patch images for buttons etc ... you can also make your custom drawable it would be easy and handy to work on ..Also you can take dpi for each screen by using switch and scale it the layout accordingly.
As, in one of my project I had used this technique for showing respective thing to each resolution device .
DisplayMetrics metrics = getResources().getDisplayMetrics();
int densityDpi = (int) metrics.density ;
switch (densityDpi) {
case (int) 1.5:
break;
case (int) 2: //1.75 will be 2 in INT.
break;
default:
break;
}
also keep all the values you are going to used for margin padding etc values-sw600dp for tvdpi tablet ,value-sw720dp for tablets
Last but not least keep all thing generic as much as you can and put it in drawable ..
I have seen some ppl who used background patterns of different dpi's and put it in respective drawable .. if there is such thing like pattern make your custom drawble and repeat it accordingly
that will save your time .. hope it may help you
In order to stretch all the images to match phones you can specify the image size using the sdp size unit. This size unit is relative to the screen size so it can fulfill your requirement.

Android Text Scaling on different resolutions same density

I have 2 devices, a 1024x600 7" tablet hdpi running Gingerbread and a hub attached to a touchscreen which is 1920x1008 22" in size, hdpi running ICS. The Android OS seems to consider both as "large" (240dp).
So, they have the same actual density (240dpi), same generalized density (hdpi), same generalized size (large) but different actual size (7" vs 22")
The text and spacing dimensions that I specify for my layout work great on the 22", but then on the 7" they look enormous and dont fit on the screen.
I've tried using dp and sp, no difference as I think the problem is that Android sees these things as the same size / density. Does anyone have any recommendations on how I can be able to scale sizes appropriately?
This program wil also eventually need to be supported on a 4.5" handheld as well.
Thanks in advance.
Sorry, my previous answer was completely wrong = )
Ideally, you should be able to design for the 7" tablet and have your layout scale up to the TV. But if that doesn't work you should be able to use something like layout-sw1008dp. The "sw" prefix allows you to specify the minimum dimension of the smallest side of the screen - so in the case of a TV, the height.
I am also facing such problem in my application. But i found a good solution for this.
I have only one layout for tablet and directory name is layout-sw600dp.
Now, when part came to height and width problems, I have created several different values directory in which i place dimensions and font size and other stubs. So there will be no constant value in layout of tablet screen.
androd:layout_width:"60dp" // i drop this scenario
androd:layout_width:"#dimen/tab_width" // i used this scenario
and your values directory name will be like
values-xlarge
values-large
All the values will be fetched from your values directory. It will not create different layout, but one layout can be used multiple times.
See my stack answer which may help you.

Categories

Resources