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
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
In my android application, I have a problem that when I run my application, its not fit to the emulator and some part cut on it like below:
And also you can see below I have a button also on it like below image:
But its not show me the full image, means a button is missing on the emulator.
How can I resolve it. kindly suggest me, waiting for reply.
Thanks
Basically, your designer and emulator are using different screen dimensions.
You can change the designer to use the same screen dimensions. Currently, from your screen shot you're using the Nexus One layout. From what I can tell the emulator doesn't look like the Nexus One screen dimensions.
With Android it isn't as simple as 1 layout for all dimensions, screen sizes and devices. You have to implement multi-screen support to address this. You can usually do this by having a variance on dimensions, styles and layouts. A lot of the time one layout will work universally, but dimensions often need tweaking.
See http://developer.android.com/guide/practices/screens_support.html for more on Supporting Multiple Screens.
It is a problem that can occur in real devices (remember, we have loads and loads of different screen sizes for android) so you definitely need to fix it.
The best way to avoid this is to wrap the entire layout inside a ScrollView, so the user can scroll to see all the content of your layout.
If you are trying to fit all the elements on the screen then you need to account for different screen sizes and densities. In your particular case you are building the screen on a different sized screen than the emulator.
Assuming you do not want the screen to scroll (which is solved using a ScrollView), then to fix this, you can do the following:
Build different XML layouts for different screen sizes
Use dimen files to specify different sizes for some or all elements on the screen so that they re-size depending on the screen size.
Use weighted LinearLayout so that Android re-sizes the elements according to the screen they are placed on.
Each of these is pretty easy to search and find various ways of doing.
I'm working on the layout of an android application. But I have a question about the layout. Once i have created a layout and then go to another screen (for instance 4 inches to 5 inches) all my buttons and text boxes are messed up..
Is there a method to make everything static, so that my layout works on every mobile phone..
What kind of layout are you using, could it be RelativeLayout? that might be your problem in this particular instance.
Anyway, in android there are many screen sizes, each of them you should handle with different layouts depending on orientation, size and density. There is no magic formula as far as I know, but 9-patch images can help a lot. Another thing you should do is make sure that you understand how match_parent and wrap_content values work for height and width.
Check this out: Suppoting different screens
You have to create folders for different types of screens take a look at android developers training
This document contains everything you need.
Instead of having everything fixed, it shows how to design a layer that adapts to the various screens you may encounter.
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 have just finished designing my android app and when I tried changing the resolutions, to check if my app looks the same for all devices, I could not notice that the app's design is twisted in all of the resolutions with the exception of the resolution I design for.
Long story short: What can I do so my app will look the same for all resolutions and devices?
Well, there are a couple of issues you could be having but without examples of what's happening it's hard to tell where you are making the mistake.
Number one could be that you are using standard pixels instead of dip or are using absolute layouts or one of the many things mentioned in
http://developer.android.com/guide/practices/screens_support.html as referenced by both hardsky and bos.
Another alternative could be that you are simply thinking about your layouts incorrectly. For instance defining a views padding from the left in order to center it in the screen will totally fail when put on any screen with doesn't end up having dimensions which pan out to 320x480dip. Try to ensure that you are always defining your view placements by their intentions directly rather than a secondary attribute which pans out to be the same on standard resolutions.
May be different layouts for different resolutions?
http://developer.android.com/guide/practices/screens_support.html