My question revolves around the general problem represented by a specific example.
When I create a button with the following markup:
<Button
android:id="#+id/btnLookup"
style="?android:attr/buttonStyleSmall"
android:layout_height="match_parent"
android:layout_width="40dp"
android:layout_weight="1"
android:enabled="false"
android:text="#string/go_button"
android:textColor="#color/black" />
And deploy the app to an API Level 10 device, the height of the button is considerably less than the height of the same button when deployed to an API Level 17 device - that is, the smaller button does not fill the height of the other items in it's horizontally aligned liner layout whereas the larger version does.
This may have to do with fill_parent vs match_parent, I'm not sure.
The problem I'm having with this is how to define a layout that makes fundamental properties like this the same across devices.
If the answer is, you don't - you use multiple layout files then my next question is what resources do people use to iron this stuff out? For example, is there a list somewhere of the differences between api levels so that someone could consult it, our do others just suss these things out through trial and error?
The difference in rendering of a layout from one device to another should not be related to the version of Android running on the device. The difference, however, can be(and mostly is) due to different screen densities and dimensions.
This problem is (to some extent) nullified by using dp(density pixel) instead of px(pixel) while dimensioning views. You can further reduce the oddities by using different dimens.xml for different screen densities. This way, you will have a dimens.xml inside values-mdpi, another one inside values-hdpi, and yet another in values-xhdpi.
I prefer another approach. I like to handle positioning of items using the screen dimensions. I get the screen width and height at runtime, and programmatically add margins and padding to layout items.
// Get screen dimensions
// Add a ViewTreeObserver.OnGlobalLayoutListener() to the view
// Update height and width for items
// Position items using screenWidth and screenHeight
This, kind of, fool-proofs the rendering. Not sure if this approach fails in some case.
Edit:
You can read through 'Providing Resources' to know about the criteria that android uses to decide on the best possible resource for a given device/situation.
Related
One of challenges in developing android applications is compatible app's UI with different screen sizes, Now i'm using smallest width in dimens and specifying sizes in DPand SPfor multiple devices for make my app UI compatible with diffrent screen sizes which solution is better than using smallest width to compatible app's UI or what is you solution for that?
I think, there are many ways to achieve this what i have learned in two years I can summarize here,
Use in-bulit width height - i.e. Match_parent and Wrap_content
For fixed width height - One can you Linear layout and divide it in screen %
If there are shape which need to be shown like circle or anything create layout, or instead of using images for gradient create them
Try to minimize use of images and incase use
them, put all vectored images in respective folders.
For some layouts - Like showing circles overlapping as per width , try to do
that programmatically where need to get height and width of screen and
then need to lill bit of math to make it dynamic
Use dp for widgets and views height width parameter Use sp for fonts
Put all in style,dimen respective pages and use from there
There are many standard things which need to keep in mind while creating UI like heard size is 44dp etc. https://developer.android.com/guide/topics/ui/look-and-feel/
So its just matter of experience just avoid fix values and for even for simplest things google how other ppl have done it try to get their views and you're good to go.
I have a design that I need to follow in my app. In it there are several buttons, images and textviews placed all over the screen (some are aligned to the left, some to the right, some are centered etc). What I need to do is make all those elements appear EXACTLY as they are in the design image. The problem is, if I try to do this using dp, it can be wider or narrower than the design image specifies, and it depends on the properties of the screen. (I've had an app where I did it all in dp, and on my high-res phone it works perfectly, but on my friend's older phone it cuts away the edges of the activity)
My question is, what elements do I use for this? I've heard of using weight in Linear Layouts, but how to make items different width and height and position them all across the screen how I see fit? How to make sure it looks exactly the same on all screen sizes?
Thanks in advance! :)
Edit:
The point is, I need the buttons to be just as they are on the screen (this will be a listview element, I'm trying to make an xml for it). The distance, proportion, everything, it needs to scale to the width of the screen and be this size and distribution. And, I'm not just looking for a solution to THIS particular problem, I want to learn how to do it in general...
You don't. For very large and very small screens, you use separate layouts that scale the sizes, completely drop parts of the UI, or lays it out in a different manner. It's absolutely silly to think you can fit all the stuff on a 10 inch tablet and a 3.5 inch phone. If your designer expects that, tell him he's an idiot and he needs to get back to work.
For a more general answer on the best way of laying things out- it depends on the effect you're trying to get. You should never use pixel counts, and dp should only be used for small things- a bit of padding between 2 fields. Most things should be done via layout, either by using a linear layout and getting things in rows/columns, or a RelativeLayout and describing how to layout views relative to their siblings. But even with these tools you will not be able to fit on all screens and look good.
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 have to position the images on my layout such that it does not change its position with varying screen resolutions. They should be exactly where they are placed. layout_weight attribute can be used with linear layout. But I am using relative layout and I am dealing with cropped images. Any solution on how to position the image views? Setting out margin height and width is not worth.
In short - you can't. Devices have different resolutions as well as screen sizes ratio. Thus it's impossible for the app to look same because there is no same definition in Android. The only way to design an interface - is to use bindings to other objects or parent view's borders. Try to investigate what are the rules, e.g. "to the bottom of the picture with 10dp gap" or "next to the TextView" - and it's the constants that are kept on every device.
I can't imagine a scenario where this makes sense but it obviously must exist since the Android team built support for it. The docs on the dimension type clearly state that it supports "mm" and "in" as qualifiers for millimetres and inches respectively.
That should get you what you need but I would advise reevaluating that requirement, as it would make design a massive pain.
I'm working on an android app that we're targeting for all screen sizes? How do i go about making my layouts? Shall i make a different layout.xml for each type of screen or is there any other "efficient" way of doing things?
Read Supporting Multiple Screens, specially the section "Best practices for Screen Independence".
Basic rules:
Use wrap_content, fill_parent, or the dp unit (instead of px), when specifying dimensions in an XML layout file
Do not use AbsoluteLayout
Do not use hard coded pixel values in your code
Use density and/or resolution specific resources
In practice, even if your layout will work on tablets, you will also want to provide different layouts for those extra large devices to enhance user experience.
Edit concerning your screenshots.
<ImageButton
android:id="#+id/btnSubmit"
android:src="#drawable/submit"
android:layout_height="22dp"
android:layout_width="85dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/confirmpassword"
android:layout_centerInParent="true" />
You specify here two vertical constraints that might not play well together. Instead of layout_centerInParent, try layout_centerHorizontal.
You could also give a gravity="top|center_horizontal" to your RelativeLayout. So by default the elements get centered in the view horizontally and get sticked to the top.
Also try to align firstname below btnSignin instead of username. You might be lucky.
RelativeLayout is the most complicated layout to use. If you can't get it right after some time, you could simply decide to fall back on using combinations of nested LinearLayout
For landscape and portrait mode, you can use different xmls, incase u need to display your screen according to the orientations. I have been using this method for my apps.
As for support of multiple screen, you can go with relative layout as parent node. Dont go with absolute layout.
you have to take relative layout for any dynamic screen & all measurement will be in percent(%) or u can use the property(fill parent or wrap content), by do so u can somewhat manage layout for different screen