How to position images at the same spot all android devices? - android

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.

Related

How did you compatible android app UI with multiple screen size?

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.

Android UI based on top and left property

I was working with Android UI in Eclipse and found it to be bit hectic. Designing layout using layout managers is bit time consuming. So i was wondering whether it is possible to specify the position of the UI elements based on (x,y) system i.e top and left property which is widely used in Visual Studio IDE for VB,C# etc ?
Positioning element based on top and left property would provide much flexibility.
How would that be flexible? Yes, doing layout correctly takes time, but if you do it right, it will scale properly to any screen size. If you're using X/Y coordinates, you will be hardcoding to a specific screen size, which is an especially bad idea on Android (as there are a multitude of screen sizes available).
If you need x, y positioning, you can use a FrameLayout with foregroundGravity set to top|left, and use layout_marginLeft for the x value, and layout_marginTop for the y value.
You can use AbsoluteLayout and suppress deprecation warnings in your code, but think of how will it look on different screen sizes?
I would advise to use RelativeLayout in your case.
As far as I know, there is no built-in layout that is based on (x, y) coordinates. You might be able to find 3rd party libraries that can do this for you. However, I'm skeptical that they will provide satisfactory results. Remember that Android is deployed on a wide variety of devices which include a range of different screen sizes and resolutions. This means that you can make the UI look pretty on one device using specific coordinates but it won't look very good on other devices.
Personally, I edit my UI layouts directly in the XML files. I find that this provides me better control than using the Eclipse UI editor. You still have to learn how the layout managers themselves work.
Android tries to ensure that your layout components are arranged nicely so that they:
don't overlap with each other
don't go off the screen space
look similar on different screen sizes
etc
It gives you nice XML Attributes to help you arrange your layout. I would recommend you use RelativeLayout for this application, because it allows you to put your layout components in positions RELATIVE to each other.
Some XML attributes you can specify are given here: Android Reference, RelativeLayout.LayoutParams

Layout looking the same across all devices using RelativeLayout

I'm using API 10.
I want to make my layout looking the same across all devices. I can achieve that by using LinearLayout with weights. It works perfectly. As seen on the image below, this is exactly what I want to achieve and it works perfectly:
But the problems that occur are two:
1)I get a warning of low performance because of nested weights.
2)I can't make animations out of bounds of the parent LinearLayout,
as shown in the image below:
So, I decided to make my layout with RelativeLayout. But the major problem is that I can't get the same layout across all devices, like LinearLayout and usage of weights. I spent two days searching for a solution and reading the ADT documentation. According to the documentation I have to make my ImageViews scaled to a factor of 3:4:6:8:12, to match the ldpi/mdpi/hdpi/xdpi/xxdpi. So I did: I created the corresponding #drawble folders, re-sized my buttons as the document says, and placed them inside the matching folders. The result is this:
This is a mess, it is totally unacceptable, for two reasons:
1) Screens may be of a certain density, for example hdpi, but they
differ from device to device of a certain screen size.
2) When I use the dp units relative to an element, for example: from
top of parent, the value I provide differs from screen size to screen
size. It is not by percentage like in LinearLayout.
So, where do I go from here? I concluded from my reasearch, that the only(or not?) solution is to make different layouts for different densities AND screen sizes. Like res/layout-mdpi-large/my_layout.xml and so on. But how do I calculate the dp for sizes and distances from relative components, based on the screen sizes? Do I have to resort to this list? Are these all the devices?:
Any tips, best practices, guides for the workflow, anything?

Android: Adjust sizes programmatically according to screen size

Is it a bad habit to get the width and hight of the device and set images/button sizes programmatically accordingly.
I find it inaccurate to use different folders for layouts and densities as it gives me wierd results on some devices (on top of the inacurancies)
Your experience is appreciated.
Thank you
Yes it is very wired thing to make the layout for the all supported screen of android. And there are lots of screen resolution available in market.
Once i have made a Demo and it Works for me. I have made one Button which height and Width is same. Now i have set its required height and width as per the one Screen in which it is looking perfect.
After that i have calculated the pixel that it required to make it Possible in that screen and based on that i have applied it to all screen.
Now it works great in all device with any density and resolution.
So if there is any view that generate at run time and you want to set its height and width then the best way is to calculate its height-width ratio and use it.
hope it will helps you.
Enjoy Coding. . . .
:)
Well, most of cases you will have layouts which are, or will become, complex, and it will be difficult to calculate the positions programmatically.
And it will be also a disadvantage mantaining it, because you will not be able to use the interface stuff (grafic layout and so on), and other people, or yourself, will not understand the calculations the same way they would if they see the views in XML. Reorganizing, changing somewhere a position could be painful.
You also will be working frequently with bitmaps, which have a fixed size, if you calculate the dimensions programmatically and stretch they will not look good. At least you would need different set of bitmaps and load accordingly.
It helps if you for example use relative layouts with rules (like above of / align at the bottom of the parent, etc), linear layouts with weights, and dip (density independent pixels). You should do programmatic layout only when it's not possible in other way. Or in some certain cases where it really-really makes things easier.

Android Design versus iPhone Design, Relative versus Absolute

I have a client who wants me to build an Android version for their existing iPhone app. The design of the iPhone doesn't use any native iPhone elements. It's basically some sort of grid with containing images, buttons, text, etc. Of course it was easy to make the iPhone app because of the fixed pixels widths/heights. The basic grid that defines a screen is loaded via a XIB file, and I load the custom buttons in the right containers in the grid by specifying the exact coordinates.
Then comes Android...
Our client wants to target 3 specific tablets (1024x600). They have given us designs for a ~600x980 portrait version of the app. It is not recommended to use AbsoluteLayout in Android. What is the easiest way to make sure that I can scale it on different devices but that it will look like the given design on the 3 target tablets.
One idea I had (which I'm not sure about whether I can implement it) was:
Get screen width in pixels and height
Based on width/height ratio of the design, pad with bars on top/bottom or left/right
Still do an AbsoluteLayout based on this information
I'd rather not do it this way because it sounds involved and counter to the Android way of doing things. Another issue that is created by scaling is the following. There is a bar of buttons that have a lines separating them. These lines are 4 pixels wide. Obviously, when you start scaling, this is going to mess this up completely. I can't seem to find much information about this s
You probably want to start here:
http://developer.android.com/guide/practices/screens_support.html
But quick points are probably
Do not use an absolute layout. Your life will become terrible
Handle sizes in density independent pixels so they will scale properly on different devices
Use ninepatch images so that when images stretch they will stretch in the proper regions maintaining your 2px borders ect.
Take advantage of the different resource folders for images at different densities (drawable-mdpi vs drawable-hdpi) and layouts at different sizes (layout-small vs layout-large). The latter will allow you to have separate layouts for your tablet devices.
Best of luck :-)

Categories

Resources