Android Element Positioning in an Overlay Activity - android

I have an idea for an application for Android. I have a decent background in Android but I have never encountered a problem like the one I am currently facing.
Background
I am creating an overlay activity on top of WhatsApp. When you launch WhatsApp a red circle shows on the overlay activity and it needs to be entered exactly above the "new chat" button in the latest version of WhatsApp (Android Lollipop).
This is easy when positioning with DP on a specific device but this circle will not be in the correct location if I test the app on my LG G3.
The obvious answer is that I would create a xxhdpi layout too, but the problem is that this circle needs to be dynamic, so if I want it to be above the search button, it needs to be there.
Question in a sentence
My question is based on margins in DP on a specific device, how can I make sure the circle appears in the exact same position on all other devices that have the same version of WhatsApp?
Example: On Note 3 (1080p, 388 PPI) margin right is 30dp, How do I use the resolution knowledge and DPI knowledge to make the circle appear in the same place on my LG G3 (1440p, 538 PPI)
What I have tried
Firstly the dumb approach at positioning it correctly on phone one with dp and running on phone two (Failed)
Second I tried positioning according the width of screen in pixels times a percent (I.E. screen_width * 0.75) so the circle will appear always at position x 75% of the screen (Failed due to screen density? 1)
Thirdly I tried the same approach as my second attempt but instead of raw pixels I used the total screen DP and used that as my relative measure (I.E. screen_width_dp * 0.75) so the circle will appear always at position x 75% in dp of the screen. (Failed 2)
Notes
In all tests I made sure that the units I am working with are correct, for example if I had screen_width_dp x 0.75 I made sure to convert the DP into PX before setting the margins.
1 - Why would this fail? using screen resolution ratios.
2 - Why did this try fail as well?
This was tested on Note 3 (1080p, 388 PPI) as base phone, and LG G3 (1440p, 538 PPI) as secondary phone that the circle should move relatively to be placed correctly on.
Thanks!

Related

android UI inconsistent between phones

In the app I am working on, there is inconsistency between what is displayed in the Android Studio UI builder and the 2 phones I tested it on (A nexus 6p and an Xperia XZ Premium. They both have the same aspect ratio, with the 6p being 2560x1440 and XZ being 3840x2160 but scaled down to 1920x1080 during normal use if I understand correctly)
For instance: I have 3 buttons with a static width in dp at the top of the screen in a constraintlayout, and in the UI builder they go from one end of the screen to the other, each constrained to the left and evenly spaced. In the builder and on the 6p, it displays properly. However, on the XZ, it is but off.
After further testing, it seems the XZ always cuts off around 25dp on the right and bottom when the layout contains objects with set widths and lengths that cross into those missing 25dp. How do I fix this? The 6p displays everything just fine
Try using auto layout constraints to help adjust the dimensions. Something you should consider for the future is that you need move fragments, that will insure consistent UI over many versions of android phones and tablets currently in the market.
Android layout view can help you do all that.
Right click not the textfield/button that you want to set to a specific constraint and let the magic happen.

How to convert PX into DP in Layout

Designer has given me a PSD file, He has created it for 1080X1920 resolution
In PSD he defines that the header height should be 52px, Slider height should be 350px and so on..
In layout when i write layout_height=350dp its taking a lot of height and covering more than half of screen.
I am using default layout to accomplish this task.
Kindly guide me how to define Height in layout in DP with respect to PX
Looks like there's a lack of designer-developer flow. The designs you were given are not mobile friendly (Android neither iOS). I recommend you to take a look to a couple of tools for a nice workflow.
Sketch
Zeplin
And please read this article you both:
Designer's guide to DPI
In Android you need to work in dps and you need the designs to be given in dps. In case you are given the designs in pixels you have to have the agreement with the designer that 1px is equals to 1dp, in other words, the designer is working on MDPI basis where 1dp is equals to 1dp.
So the easiest way to go is your designer needs to work in a screen that is 360x640 pixels instead of 1080x1920. Now you can forget about translating pixels to dps and the other way around because 1px would be 1dp for you. For your designer as well will be easier because he needs to go from guidelines to the design tool, and he doesn't need to translate dps into pixels anymore. Everytime he reads 48 dps in the guidelines (which is everywhere) he knows he has to read it as 48 pixels in his 360x640 canvas in the design tool.
Notes
360x640 is a common screen size in dps (Galaxy S5, Nexus 5 and many others) and because of that is taken as a reference now a days. Please realize your designer can not give you the exact sizes for every component in every layout size for every device. So things like the toolbar height (usually 56dp) is easy to translate to every device, but the toolbar width that you'll size in his designs (360 if you follow my recommendation) is completely useless for you, because that depends on the real device. The Galaxy S5 and the Nexus 5 are 360, but the Nexus 4 is 384, and the nexus 6, 6P and 5X is around 413.... and actually there's much more!!
Designing just for one device or one screen size is too simplistic and problematic. Be aware you have to develop responsive layouts ready for a multiwindow.

Android Scaling for Multiple Resolution Screens

I am developing an Android application where the server sends all the values corresponding to dimensions in pixels for 1920*1080 resolution device.I need the app to be supported on multiple screen resolutions.I went through Android documentation on supporting multiple screen resolutions.It suggests to convert pixels to dip and then render.I did that in my application but the views are not rendered as required.So I tried applying simple unitary method by dynamically getting the screen width and height and then scaling all dimensions based on current screen width and height.
Say my current screen width is X and height is Y.So what I did was
Scaling factor in horizontal direction = New Screen Width/1920.
Scaled dimension in horizontal direction = Scaling factor in horizontal direction * Dimension from server in horizontal direction.
Similarly for vertical direction.
The application is now looking fine on my device.But is it a reliable way of doing things ? Should I be dealing with density of display too ?
DP is probably the better approach, if you elaborate a bit on what you mean by 'not rendered as required' I can try to help.
I can think of two main issues with your current method:
Different aspect ratios of devices. Using your method you will end up with distorted imagery. For example a square in 'server dimensions' is 400x400. In a 800x480 phone, that square will be 162x177 - no longer a square. Depending on your visuals and purpose of your app, this may or may not be an issue. If it is an issue, you need to account for that.
Physical views' size. One of the purposes of the DP method is to ensure a view will have (almost ) the same size across different devices, mainly never too small to handle for the user. So using the DP approach, a 100dp button will be 200px on a high density device, and 100px on a medium density device. This way the button is physically the same size on both devices.
But your method ignores that. Take the square from the first example - it will always be a fifth (400/1920) of the width of the screen. This can be either huge or tiny, depending on the device dimensions. Again, depending on your needs this may or may not be a problem.
Your method can work, as long as you account for these (and maybe more) problems. But it does require special care and probably more coding and work to make it work perfectly compared to simply using DP.

Different Text Size under same screen resolution but different dpi in two Android machines

I have two Android machines - Samsung Galaxy Tab and HTC Flyer. They both have 1024x600 screen. However Tab has 240dpi screen while Flyer has 160dpi according to the log from Context.getResources().getDisplayMetrics() method.
My problem is that, I have a TextView defined, and according to those Android document, using unit of dp should give same physical size under different screen density(dpi). But what I observed is that, the text size appeared in Flyer is obviously smaller than that in Tab, no luck in using sp too. I want them to be in same size. Any clue in solving this problem in general?
Thanks in advance.
Well, the reported values are clearly a lie. I suppose the Galaxy Tab is the 7in model. This means that, based on the reported values, it has about 4.2in display height (1024 / 240 = 4.2in) and about 2.5in width. This means that the screen size would be 4.2x2.5 in - and in this case the diagonal would be about 4.9in. Is it correct - no it isn't, it has 7in display. Measure the sides of the screen and you would get the correct density. So, even when scaled by Android the result will be wrong because of the false density measurement.
Anyway, the answer of the question is:
No, no way to draw same-sized fonts, images and whatever. You need to get used to false metrics from the manufacturer. A false density reading reflects on all other scaling. Thus even mm/in would not help.
And one advise - don't try to match sizes between different products, unless absolutely necessary. If you succeed, then the text on the Galaxy Tab, for example, will look disproportional in regards to the other text on the device and will make your app look out-of-place. Stick to the Android textAppearanceSmall/Large, etc.
Please try the following units:
mm
Millimeters - based on the physical size of the screen.
in
Inches - based on the physical size of the screen.
http://developer.android.com/guide/topics/resources/more-resources.html

Android UI problem

I'm required to maintain the same Height in Spinner and EditText views, but when moving to a smaller screen and having the width as "fill_parent" or "wrap_content" the view controll increases it's height, so I tried to fix the height with 50dp (and 50dip) but the views just gets cut.
Some pics to illustrate my point
Second http://img218.imageshack.us/img218/6776/screenshot20110527at330.png How it should look (Bigger Screen)
Third http://img171.imageshack.us/img171/6776/screenshot20110527at330.png How it looks when fixed 50dp (Small Screen)
First http://img151.imageshack.us/img151/6776/screenshot20110527at330.png How it looks in with "fill_parent" & "wrap_content" property (smaller screen)
These are from emulator, right? Emulators don't scale well on computer screen. Just use mm and know that the size can never be the same in the real world. You will have to get used to variable size. Reasons - different screen densities reported wrong from Android (phone). Even today you can never make equal sized text on computer screen. So, next time you're asked to make an image X cm size, just laugh and ignore.
It will look OK, yes. But will not be the same size. The most important reason is that phone screen have different pixel densities that are reported wrong to Android and as a result the OS cannot scale properly.
See this thread:
Android: how to draw an ImageView with the same size regardless of device?
Use mm or inch , and will be the same physical size for any device

Categories

Resources