I have finished my application and now need to handle adapting to multiple screen sizes.
At this point it seems my only option is to do the following:
Have a seperate xml layout for: Small, Normal, Large and X-Large screen sizes. Have a seperate xml layout within that for: ldpi, mdpi, hdpi, and xhdpi Have a seperate xml layout within that for portrait and landscape.
That is a total of 32 layouts for one activity.
I am just wondering if there is a better way to do this as this would be very time consuming, however if it is required to support all the possible devices I would not mind it.
I did some reading and people said to use Linear Layout and use gravity as that is a percentage so it will look the same on all screens. Or I could use RelativeLayout and use "AlignAbove or Below" etc.
However my main problem is that the sizes are wrong, while the location of the elements seem to be generally in the right place.
Is there a widely accepted way to do this?
I am really just looking for what the "main stream" way that the average developer handles all screen sizes.
LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.
Relativelayout is more effective than Linearlayout. It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.
Good question. But that way you are doing is not how a "professional" developer would do. You do not need 32 layouts for a single Activity, Android is intelligent enough to handle things.
Without multi-definition of layouts/multi-definition of dimens.xml multi-device support possible or not?
Yes, it is absolutely possible and this is what you should be doing. Defining 32 layouts for a single Activity layout is not a good thing.
Suppose, I have a considerably large app in Play Store with more than 20 Activities, now if I had defined 32 layouts for a single Activity, then there would have been more than 600 layouts for my app. Is that feasible? Just ask yourself.
You need to start by giving this page a very very good read. It contains almost everything you need to know. But I will help you get things right.
You do not need to provide alternative resources for every combination of screen size and density. The system provides robust compatibility features that can handle most of the work of rendering your application on any device screen, provided that you've implemented your UI using techniques that allow it to gracefully resize
This statement is enough to convince you that you should not be doing this. The Android system is intelligent enough to resize your layout accordingly. And trust me, its really good in that.
As you design your UI for different screen sizes, you'll discover that each design requires a minimum amount of space. So, each generalized screen size above has an associated minimum resolution that's defined by the system. These minimum sizes are in "dp" units—the same units you should use when defining your layouts
I will try to point out some of the important things you need to consider.
dp
or sp
in your layouts. As the name suggests, they are independent of the density of different devices, so will look same in every device.wrap_content
or match_parent
for heights and widthsAbsoluteLayout
RelativeLayout
or LinearLayout
both would work great for you, you just need to decide which one you want to use depending on the scenarioAndroid Studio Rich Layout Editor
Android Studio has an amazing layout editor where you can preview your layout instantly. Just see this screenshot below,
I am previewing a particular screen of my app in a variety of screen sizes all at once and my layout looks just as I wanted it to look, in all of them.
I just have two layouts for this Activity, one for all the phones and one for all the tablets.
Practical Experience
If you want a practical experience, I have an app running on the Play Store which is used by thousands of users on more than 8000 different devices of different screen sizes.
I only have 2 layouts for a particular Activity, one for the phones and one for the tablets. The UI of my app looks good in all devices and I never had a complaint about it from any user. In case if you need, here is the link to the app.
I am sure this detailed answer could clarify all your doubts, if not, do not hesitate to let me know. I would love to help further.
UDPATE
I would try to clear your confusion once again. But firstly I would RECOMMEND you to read my answer more than once.
Either you need one layout or two, it all depends on the particular layout you are designing. All layouts are different from the other. It can't be defined technically as its subjective. There is no thumb rule for it. But I will try to help you formally.
You need to make good use of the Android Studio Rich Layout Editor and its awesome live preview feature.
First try to design the layout targeting any average device size (like 5 inch of Nexus 5). Just choose Nexus 5 to preview your layout.
Once you are satisfied with the design, select other screen sizes to preview them. You can also choose "Preview all sizes" like the screenshot above.
Try to see if your layout looks good in all the screen sizes, varying from a 4 inch to a 10 inch tablet. See if the spacing's, image sizes, font sizes are all correct.
All the phones ranging from 4 inch to 6 inch will generally have perfect previews, but you may need to design an another layout separately for the tablets screens (as they are large enough). Only here comes the need for 2 layouts, but not always.
Like in my app, I have 2 separate layouts only for 5-6 activities, but for the rest I just have a single layout. It all depends on the particular layout.
Designing is a whole lot different from programming. Programming can be defined formally and grammatically, but design can't be. Design is absolutely subjective and depends on the designers perspective. Be creative in your own way, that's the magic of design.
However my main problem is that the sizes are wrong, while the location of the elements seem to be generally in the right place.
Are you using dp and sp units correctly?
dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
from android documention.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With