I am trying to make a application in Android. I want that it should be able to run on multiple phones of different screen sizes, so i studied support multiple screen on developers and according to that i have to create 3 different xml files for supporting three different screen sizes and also 3 different types of images for each type of xml file. But on a blog i get the idea of doing this by using current screen size method. So i am confused what i should do. means which is optimized and performance increasing way. And which one will be more perfect for supporting all types of screen(except extra large screens)
Defining height, width and other parameters in the XML file is the better option rather than on run time.
Because XML files works as metadata (data carrier) to the activity and avoids alot of confusion when onCreate mothode in called.
Plus, create different folders for image quality (hdpi,xhdpi,ndpi,ldpi)
7 inch device use mhpi
10 inch devices use hdpi and xhdpi
While NEXUS tabs use hdpi and xhdpi irrespective of their size.
Mobiles use ldpi and ndpi.
Beauty lies here is that android device automatically pick-p the suitable content when found, i.e layout and image.
If not found it would first search other Layout folders,e.g a layout not found in x-large folder then it will search in large,then medium, small, which one of them suits the best ,(if a layout is not found in its respective folder).
Nexus will create alot of trouble for you.
To check how your layout would look on different devices, try using the options, which tells you how it would look on that device with those height width, present in the Graphical (view of a ) layout.
You can use three different layouts for different screen sizes ,and android will pick the suitable layout , but Using three different layouts for each type of screen format will not be a good idea , because it will cause problem in handling all layout , if screens are less then its fine but if number of screen increases it will get difficult . Like if you forget to add change in one of the screen size it shall crash with any exception .
What you can do is keep images of different size in different folders and practice layout to make standard in one layout by using layout weights , and margins in in dp .
See my this answer
Table Layout spacing issues
and check this layout will look similar for all screen sizes.
If your design is same for all screens sizes you can use dp and have only one xml for all screens.
But you should support icons for all screens.
I think it's less confusing David Ohanyan way, but forgot to say something...
Whenever you can, use styles in your xx_layout, images, etc, so you'll have 1 layout.xml and 3 styles files inside folders: values, values-small, layout-large.
At least for me, it's less confusing than opening 30 different layout files.
Related
I am a little bit desperate because I'm trying to know how can I know the exact list of different dimens.xml(hYYYdp) I have to create in order to support all the screen sizes. I think I have already seen all the related questions to mine but none of them has helped me to solve this doubt. This is the most related question that hasn't solved my doubt: How to define dimens.xml for every different screen size in android? .
To give you some context. The reason of having different values according to the screen size is because I have a ConstraintLayout with some items that have Top and Bottom margin and in some devices that margin is not looking good. In this post I made, someone suggested me to use different margin values according to the screen size: How to solve TextView being cropped in small devices because of layout margin?
I have thought that as the margin is dependent of the height of the device (as I am using only Top and Bottom margin) I would have to create a list of different dimens.xml files according to the height of the device (hYYYdp).
The real concern I have is that I don't know how many dimens.xml files I have to create to support all the screen sizes. What I have been doing is creating some specific dimens.xml files that are working for specific devices, but I think there has to be a general list of all the dimens.xml files that have to be created to support all the screen sizes.
For example, what I have been doing is to look the test devices of Android Studio one by one and create specific dimens.xml files for them. This is the process I have followed to create a specific dimens file:
Example:
1- First I have seen the list of the test devices of Android Studio, for example let's take this one:
2- After this, I have calculated the dp of the height of this specific test device:
240x400px (ldpi) device. So the height's dp is = 400/(120(ldpi)/160)== 533.33dp
3- Finally, I have added the dimens.xml(hYYYdp) correspondent to this specific device with the specific margin value that is looking good:
I have repeated this process for most of the test devices of Android Studio but as a result, I have a list of specific devices but I think there will remain many devices without being supported as not all the Android devices are in the Android Studio test devices list. I hope you have understand what I am trying to explain.
Which is the layout to use which doesn't have a difference in different screen sizes. Linear layout or relative. I had a relative layout and when I run the app on a 4' phone, the icons were way closer. In a 5' phone the icons were a bit away. What's the best way to have a constant XML which doesn't change by screen size.
For example.
I had this app in linear layout which contained 3 images in horizontal orientation. In the design view the icons were perfectly same and equidistant. But on the device, the third icon was smaller. Why
You can't achieve perfect layout for every device with just single layout file. For best results you should make different layout files for different folders used with images designed accordingly. For more information read Android Developers' tutorial from the following link
Supporting Multiple Screens
Over and all, you need to create different layouts/drawables for supporting all the screens. It depends on your requirement, say for example, if you would want to include 2 buttons in small screen and 4 buttons on large screens then obvious you have to create different layouts.
If you would want to display same number of buttons in all screens then I would suggest you to prepare different set of images/drawables and include them in your project. Here you don't need to do anything other than placing images in particular drawable folders.
One more thing, If its a plain background then you can create 9-patch image so that it can stretch with whatever size you want or your device supports.
More study:
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/screendensities.html
http://developer.android.com/training/multiscreen/adaptui.html
I am developing an Android Application that supports all kind of Android devices like mobiles and tablets. But it's time consuming to create multiple folders (small, normal, large and xlarge android xml layouts) to support all the Android devices. Is there any way to build an android app that runs on all android devices without creating these folders:small, normal, large, xlarge etc?
You only need to create unique layouts (ie. *layout_mdpi* ) if you want something unique for that particular screen size.
If you want to use the same layout on all different screen sizes, you will only need to create a single layout (in the layout folder).
Only if you want to customize a particular layout would you need a new FOLDER in layouts (named: layout_mdpi) in that folder you would have multiple copies of customized layout with same name (ex. my_layout.xml)
To clearly answer your question - you will only need the layout folder and no other ones in your casel
Ex.
res\layout\my_layout.xml // this folder is all you need if this layout will work on all screen sizes
res\layout_mdpi\my_layout.xml // you ONLY need this if you are presenting something unique on this screen size.
In Android we need to maintain different folders for the layouts with different resolution reason behind it is the use or the resolution of the Android Device on which the application gonna execute.
small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
Android OS select the specific layout it self by checking the compatible device and its resolution.
So, better to create folders to support in multiple screens
For More Info refer this
Is there any way to build an android app that runs on all android devices without creating these folders:small, normal, large, xlarge etc
Consider this, you have a button (with match_parent) that stretches full width of a screen in portrait mode of a 4 inch phone, that's fine it looks alright, but then that same layout on a 10 inch tablet in portrait is now 3-4inch wide, that's not great looking.
"So what" you say, make it wrap content, okay then so the button now only fills up part of the width on phone, still looks okay but then on a tablet you have huge amount of space now either side of the button, maybe that's looks okay, maybe not.
Maybe same button on a smaller screen takes up too much space?
Now apply the above to every single layout element in your app.
Do you think it'll look good, using the same layout, do you think your users will be okay with an app that was so little care to its UI and UX?
SO, in conclusion, yeah it's possible to only use one eg normal, for all devices but it'll probably look terrible on most of them.
I am working on a app in which there are some textfiels and button when I am running it on different screen size devices it is showing unexpected result as shown in the below image!
[Image 1] http://oi45.tinypic.com/25yvon4.jpg
[Image 2] http://oi45.tinypic.com/xmlqns.jpg
Here in first image some space is left vacant at the bottom due to large screen size and in second image the last row of buttons are hidden due to small screen size.Also for buttons I am using Table Layout.
Is there any way to solve this problem.
That's a broad topic that needs spatial understanding first.
Here's a good place to start - http://developer.android.com/guide/practices/screens_support.html
Long story short, always use point units (dps), try to avoid to hardcoded pixels within your code, position items in a relative way (ie.: in relation to other object - RelativeLayout, LinearLayout, etc) and make sure to take advantage of the power of "values-..." folders.
Example
values-xhdpi (XH)
values-hdpi (H)
values-mdpi (M)
values-land (Landscape)
values-sw600dp (smallest width at least 600dp)
....
Use layout, layout-large and layout-xlarge resource folders to customize your layout files if you care about various screen sizes. Test these layouts on phones, 7" and 10" tablets to cover all of your bases.
For small phones of less than 4" screens (qvga) as well as older devices (wvga), make custom layout files (in the layout folder) and refer to them dynamically in your java code when you detect these kinds of devices. Again, test on these older & smaller phones as much as possible. Borrow some phones from the Sony Device Loaner Program in order to get real-world testing done.
Lastly, use ScrollView to embed your layouts if you want vertical scrolling on smaller screens. Don't go crazy trying to fit everything on a smaller screen. Sometimes scrolling is a natural solution that your users will understand.
You must have to make screen for all devices if you want to solve your problem.Read below document for different screen:-
http://developer.android.com/guide/practices/screens_support.html
or
You can use weight or layout weight to prevent this problem.
I realise this topic has been covered to death, and I have read the official and unofficial developer guides on it so know the theory. But I'm still unsure which path to take to ensure my app looks good on multiple screen sizes. My app is a simple navigation style app which is designed to be portrait only. At this stage I'm not interested in making different designs for landscape or multi-pane screens for tablets etc. I just want each screen to be scaled up/down so it looks in proportion with the screen size it is being displayed on - i.e. text and images are sized up/down as appropriate. I designed the app for a classic 480x800 hdpi device which it looks great on, I just want to make it look in proportion for the Galaxy S3/Nexus 7 etc. I'm already following these guidelines :
Using RelativeLayouts
Using DP units for padding/margins
Using SP units for fonts
Using 9-patch images for buttons
However I'm finding it still looks small and a bit lost in the middle of the screen on the larger devices. In particular, my main menu screen is a grid of 6 image buttons which I can't get to scale well. I don't want to have multiple copies to maintain of the same screen (normal/large/xlarge), just want one layout. I was considering the following :
Using value-normal/value-large etc. folders to store XML files with DP/SP values for sizes of images/text
Changing RelativeLayouts to LinearLayouts with weights (although I initially struggled with LinearLayouts and changed to RelativeLayouts
Are either of these valid approaches, or is there a better approach?
Personally, I would go with the first option. While this will work well for phones, it will make the layout seem somewhat bloated on tablets. Hence, another approach would be to create separate layouts for tablets and place them in the layout-large and layout-xlarge folders for 7" and 10" tablets respectively.