consider a requirement of an app which need to be run on different screen sizes. lets say app has text fields ediboxes list view etc... and in each screen size the width/height of each ui component has to vary. how to desing an UI for this kind of requirement. please suggest.
Use Width and Height as fill_Parent or wrap_content..
if you are using images make different sizes images and put it in drawable-hdpi,drawable-ldpi,xhdpi,xxhdpi.The Sizes for those imagaes are in developer.android.com
In the res folder,there exists folder named layout,that the default folder which the system find the layout defination.But people also can make other folder like
layout-480x800
layout-1024x768
and so on ,and put the layout files in it.
When the app run on a device which have the certain size,the app will auto find the layout defination in the same folder,if cant it will use the default defination.
Related
I am getting very confused on how to support all different Android screen sizes. I have already checked this answer, this site, the official documentations and many other answers but still I am confused so please don't mark this as Duplicate.
I already created layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, layout-sw480dp (I only need to support up to 6 inches so I don't create layout-sw600dp).
What do I have to do is just have one layout and just copy it in each different folder so Android will do the selection on its own or I also need to change values in each layout?
What do I have to do is just have one layout and just copy it in each different folder or I also need to change values in each layout?
You start by deleting the layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, and layout-sw480dp directories that you created. Or, at least, ignore them for now.
You then start implementing your UI, putting layout resources in res/layout/.
Then, for those layouts that need to be different in certain scenarios, you then create a directory representing the size when you want a different layout. Copy the layout from res/layout/ into the new directory, and then modify that copy to reflect whatever changes you want.
In other words, one copy of every layout is in res/layout/, and you override where needed with additional, modified copies in specific directories where you need the UI to change.
If you want to use the same layout for each and every screen density, you don't need to create different folders. Use just one simply called "layout" and the system will interpret it for every density. However, this could lead to strange layouts on certain physical devices depending on their screen size and density...
Another point you have to be aware of, if your application supports orientation changes, is that you have to design layouts for portrait and lanscape orientations. This is done by duplicating a folder used for a density and add "-port" or "-land" to inform the systen which one must be used according to the actual orientation of the device your app is currently running on.
If you want to precisely define your app look and feel, you have to customize your layout for each density. And if you use bitmaps, you will have to customize them either (for example, your app icon should be defined with different sizes to keep a good looking for each screen density). Just as for the layout, you have to create "drawable-..." folders to contain the corresponding versions of your bitmaps.
This is an answer that's been an issue from old ages and for which you'll see lot many answers but which is not a one fit all type still. What I did come up with though when faced with the same issue was to use PercentRelativeLayout. This is a relatively new feature that was started from Android support version 23.0 (android sdk manager), and one of the big game changers according to me, since it allowed
developers to specify their blocks relative to the layout size Percentage-wise. And since it is in terms of percent, this fits all screen sizes with varying dimensions, but while maintaining the ratio.
Ofcourse this method involves some trial and error and a lot of experimenting, but I found this method to be the easiest to implement and which took out the headache of maintaining the dimensions for various screen sizes.
Few tutorials to get you started :
https://www.tutorialspoint.com/android/android_relative_layout.htm
https://guides.codepath.com/android/Constructing-View-Layouts
I have a doubt. If i have one single xml file let's say res folder--> layout folder--> my.xml. i want to display this my.xml to all multiple screen size without creating layout-mdpi, layout-hdpi etc.., May i know what is the simple and best way to achieve this concept ??
Thanks in advance
Place the file at res\layout\my.xml
Every single device will get that layout by default unless you create an alternative.
If you have a single layout file in the default location (res/layout/my.xml), then it will be used on all the devices that your app can be installed on.
However, that's usually not what you want because it will look very different on a device with a different size than what you developed on. To handle this, you can start by creating another layout file in another directory (res/layout-sw600dp/my.xml). This layout would be used for devices where the smallest width (sw) is 600dp, ie, tablets.
I am currently writing a remote control program to control a robot on all android devices.
I am trying to display the layout to fill the screen fully on different sizes. My first try was on a samsung 10.1' tablet and it was working well but when I port it to smaller devices like 4.3' the layout goes wrong. I am thinking of creating several layouts to match with different screen sizes but how do I check which layout to set according to screen sizes?
I have tried getwidth and getheight but it only works after you have set the layout.
Please give me a short sample code if possible as I am very new to this.
If there are any other better ways please advice me on it.
Please note that I am using API level 8, android 2.2.
Go through this and this. Basically, you create layout for various screen, each with same name. They are put in different folders (each named according to factors like- landscape, portrait, screen density and screen size). OS will decide on it's own about which layout to use.
This is great article about that. But in overall, never expect the screen to be any static size. But if you're deciding to create layout for each size, you don't have to worry about choosing the best layout, android will do it for you if you provide multiple versions of same layout.
You do not have to check the screen dimensions. Just create your layouts, and Android will automatically pick the correct one for your screen size, orientation, API level, etc. See here for more details: http://developer.android.com/guide/practices/screens_support.html
I am developing an application for whole android devices. But resolation of screens are different and that is the biggest problem how it looks. So, I want to make resizing controls and also I used absolutelayout but It is still same.. I give value to controls as dp ..
How can I solve this problem ?
You don't resize the screen of an android device - you make your app instead work with the various screen sizes.
The relevant docs are here.
You cannot hardcode the dimensions of your layout and expect it to work on every screen size. And there is no method which automatically does it unless you write it.
You might want to change your approach, use Relative Layout or Linear Layout instead and use values like fill_parent and wrap_content while designing your layout.
Another approach Android developers follow is use different resource files for different screen sizes and Android loads them automatically at runtime.
Refer to this for more info on how to work with different screen sizes effectively.
I am having trouble in creating generic layouts for my application. As expected, it can be used in a variety of devices and I want it to work properly for each of them. There are several approaches to achieve this problem but I want to create an xml file (similar like web.config files) and at the very beginning of my application I want to take the device's screen width and height and calculate each control's (textview, spinner, button etc.) attributes (such as margin, padding,width, height...) according to this width and height and save these calculated values into my xml file. Finally I want to reach these values from my layout xmls so my layout's visual will be independent from the device and will work properly for each device. Can this be achieved? I could not find any similar solution on the internet. Can anyone help me?
Thanks in advance.
You can do most of this without hard coding values using RelativeLayout and similar mechanisms. The two pass dynamic layout system is made for exactly what you're describing.
However, when you need to be more specific, that's where the dynamic resource system can help you out. For everything you define in res/drawable, res/layout, res/values, etc, you can define specific implementations for device orientation, pixel densities, screen size or even language by qualifying sibling directories with the correct format. Provide a resource with the same name in different folders, and the system will decide which to use based on the runtime environment.
Give this a look:
http://developer.android.com/guide/topics/resources/providing-resources.html
I would not use custom measurements to dynamically set layout parameters. Android specifically has a variety of functionality to address this for you (including supplying multiple image resources, or layouts specific to a screen size).
I have discovered that the more you try to customize the Android layout with hard-coded values (always use DP if you do want to set specific parameters).
Bottom line, you should not try to re-invent the wheel, and just use the well-designed functionality that Android has already built-in to accomplish what you want.