I've implemented buttons and other UI elements for React Native using 9-slice scaling technique. However, I've stumbled on a problem on Android, which affects some devices - seemingly depending on their pixel ratio or pixel density. What happens is that the views containing the pieces overlap each other. I can't rely on negative margins and such to cover up the gaps as a workaround because I need to support opacity - so that won't work if they overlap each other.
How can I solve this? I've tried all kinds of methods of using React Native's PixelRatio, but I'm out of ideas.
I've posted this issue in React Native's Github repo as well (see https://github.com/facebook/react-native/issues/34654), but I'm looking for solutions since I don't know when/if that will be resolved.
Below is a simplified reproducible example in Snack, along with a screenshot depicting the problem: https://snack.expo.dev/#insats/346e2f
Here's a more complex snack which uses a 9-slice technique: https://snack.expo.dev/#insats/android-gaps-between-views-flex
Related
I am relatively new to react native and am not looking for actual code however I am wondering the best way to implement layouts/ styling of a page so that it looks the same proportion wise across multiple different devices and operating systems. For example iPhone X through to 13 have different size screens and then if you include the new Samsung's, Google Pixels and Huawei phones there is a lot of different sizes of screens.
I can create different styles by detecting what device is being used and getting the logical resolution from that however I am wondering if there is a better way than just doing this for 10 different phone sizes. For example in the picture you can see the front wheel of the car is on the edge of the blue box which gives it an almost 3D effect.[] To get this effect across 2 different devices I have to change the absolute positioning of the car for both devices however the more screen sizes/ devices I want it to look the same on the more code it is and it seems like I am using a long winded/ redundant method.
Another alternative I have thought about is just turning that section into an image and using it as a background image however I am not sure how that will affect performance.
Any thoughts?
So i was building this UI for an app in Unity to be deployed on Android Platform.
Here is the preview from the Game Window :
But when i build and run the app on my device, it shows this :
The header log is anchored on Top, the title is anchored on Center and the buttons are anchored on Bottom.
Here is the hierarchy of the components :
Im new to Unity and NGUI so i really dont know what's wrong with this. Thank you guys.
Different behavior is probably caused by different resolution/ration on your device in compare to your editor window. To emulate such behavior in the unity editor, you can force resolution/ration in your game window, or you can add your specific device resolution (what I actually recommend for you to do as a good start):
When you experiment with few of them, you will see that your interface is behaving in different ways.
To change that behavior on different screens, you can check your UIRoot options, especially Scaling, see UIRoot documentation.
Another good places to start is NGUI forum's topic concerned in handling different resolutions or official video about NGUI anchors.
Hope that this will guide you to perfect cross-device interface design.
I think your problem is configuring how is going to work NGUI with your sprites. There are 2 things you could do:
Use a pixel perfect resolution: This will maintain the perfect size of the sprite, and will look the same on every platform (this causes some things to look smaller if the resolution is higher)
Use a fixed Size of the UI and recalculate the sized based on the device.
The problem you are having here.. is because of that. The background for example, should be larger if the screen is larger.
To achieve that.. NGUI provides you with a UIRoot where you can configure the Scaling style to PixelPerfect, FixedSize and FixedSizeOnMobiles.
Try setting that up and the images will adapt to the different sizes.
Hope this works for you, I'll attach an image so you can check out what I'm talking about
I have been learning android development and I am still new to this.
I have already designed many apps for iOS.
Is there a better way to have my layouts as easy as iOS?
because in iOS I only have to drag and move it wherever I want . Where in android I have to insert tables and edit XMLs, which I find very confusing and not flexible to deal with.
I have read and watched many tutorial and still find it a bit difficult to deal with
Thanks
There is a layout editor for Android that you can use in Eclipse. Is that what you're looking for?
http://developer.android.com/guide/developing/tools/adt.html#graphical-editor
The short answer is NO. The long answer is that this is how it's done on Android, and it's much more flexible than the iOS method. Just imagine making Android app the iOS way and support screen sizes such as 240x320, 320x480, 480x800, 480x854, 540x960, 600x1024, 720x1280...and so on, and it's easy to realize that it would be an impossible task. This is almost certainly why Apple went with "Retina Display" with exactly 2x the pixel density and exact same screen dimension (3.5") - because doing anything else would mean that all the apps would need to be designed specifically for the new screen instead of just providing images with a #2x postfix.
So, instead of absolute positioning and sizing like the iOS, XML is used in Android where designers are tasked to come up with one or more layouts that can support this diverse range of screen sizes.
You can use Eclipse Layout Editor for Drag and Drop with absolute layout. But using absolute layout is not a good practice because in iOS screen size is fixed for all device but in android there are different screen size device are available and if you design a layout considering a single screen size it will be messed on another screen size device. You must follow the norms to create a layout go through this link for more detail.
http://developer.android.com/guide/topics/ui/layout-objects.html
I wanted to know if it was possible to precisely position widgets on Android using XML files? From my experiments, precise position seems impossible via XML. Programmatically, I'm able to position things perfectly based on the device width and height. However, using XML and dip values, things seem to fall apart. Here's one example of what I mean.
Mark Murphy, on an article, wrote:
Some developers, perhaps those coming from the "drag-and-drop" school
of UI development, think first and foremost about the positions of
widgets. They think that they want certain widgets to be certain fixed
sizes at certain fixed locations. They get frustrated with Android
layout manager (containers) and may gravitate to the deprecated
AbsoluteLayout as a way to design UIs they way they used to.
That rarely works well even on desktops, as can be seen by
applications that do not handle window resizing very well. Similarly,
it will not work on mobile devices, particularly Android, with its
range of screen sizes and resolutions.
Instead of thinking about positions, think about rules. You need to
teach Android the "business rules" about where widgets should be sized
and placed, with Android then interpreting those rules based upon what
the device's screen actually supports in terms of resolution.
source
This leads me to believe that precise positioning is difficult with XML files. This is important for my app because I'm laying out widgets on a background image so that these widgets need to be placed exactly on a particular area so that the overall UI looks good. The aspect ratio needs to be preserved across resolutions. Can I do all this using XML files?
Thanks! Any help is greatly appreciated!
Yes you are able to pixel by pixel position your views using an absolute layout.
Last time I checked, which was a while ago, those drag and drop interfaces actually created one for you. (Actually just checking for the link seems that this class has been deprecated.)
However, I highly advise against using them. The reason is because you are coding for so many different screen sizes that providing pixel by pixel dimensions will certainly cause problems across devices.
What I would recommend is using a RelativeLayout. I actually use this type of layout for everything that I can. It lets you put your items to right of, or above and below other elements. This combined with padding and margin options in DIP (density independent pixels) should let you create anything that you want. And it should look nice on most devices.
Are there any android zooming solutions that can be applied to a generic android view?
My app needs to push a lot of information onto the screen, and on some phones, screen is so small that this data becomes unreadable.
I want to get zooming functionality that works for textview's and image buttons in particular. I imagine a zoomView that works for these two built-in's would work for most of the others too.
I've looked around on the web and haven't found any easy solution yet. Most of the discussion seems to be focused on zooming images or a webview.
If there is not an existing solution, can anyone suggest a good approach to take?
Are there any android zooming solutions that can be applied to a generic android view?
That is not possible except perhaps on Android 3.0. On Android 3.0 (API Level 11), you have getScaleX() and getScaleY() which might suit your needs.
If there is not an existing solution, can anyone suggest a good approach to take?
Design a different UI optimized for smaller screens.
Or, use a ScrollView (or HorizontalScrollView) and use bigger widgets (effectively pre-zooming your content).
You can try to implement your own pseudo-zoom by changing the text size and button sizes based upon user input (e.g., options menus to zoom in/out), but I'm not sure how well that will work.