how to create universal UI design? - java

I would like to learn from experienced developers Android, as you develop a UI for different screens? For example, now I have this problem:
I have an element (TextView), which should be placed as nearly aligned to the left, but not back to back, but with a slight indentation. Accordingly, the bigger the screen, so this should become more padding. If it is set fixed (px / dp), as layout_marginLeft, it will remain so for all screens.
Or, like I have a button, which should take approximately the width of the percent of 60, while the remaining 40 percent are left blank. How can this be done without specifying a fixed size?
Is it possible to design a universal screen, which will stretch to compress the distance between the elements (as in my case), and do other similar things? Or is it necessary for each screen to create a resource directory and a separate design for each screen? How do you usually do that?
Thank you in advance for your reply, it is very important to me.

Some tips :
Use RelativeLayout when you want Views to be aligned with respect to each other.
Use LinearLayout with weight when you want that 60% - 40% thing
Use Custom Layouts when none of the above layouts work for you, i.e you want to customize its look and feel
It is alright to use different layouts for different screen sizes
Always use dip, - device independant pixels - instead of px
Read this article - Think like a web designer
Lastly read this article about supporting multiple screens.

Related

Programmatically set a percentage height to LinearLayout in a Widget's RemoteViews

I want to create a widget with graphs which represent a percentage increase in stock value for certain assets. I was able to create an overview of graphs with each graph having a percentage height by putting two LinearLayouts in a container LinearLayout. The bottom LinearLayout is the graph and the top LinearLayout is the spacer which gets a certain layout_weight. This way I can reduce the size of the other container using percentages. (if the spacer has a layout_weight of 0.4, then the graph is 60% high).
The current state of the widget is (I wanted to handle negative values in red afterwards, but no need to continue if I can't figure out how to percentually size the graphs) :
I chose this approach because I can't use ConstraintLayout in RemoteViews. Everything seemed to work nicely, but when I wanted to programmatically change the size of the spacer based on data from the api (so the sizes of the graphs truly match the percentage increase of the stock value), I wasn't able to. There is, as far as I could see, no way to change the layout_weight of a LinearLayout in a RemoteView in java.
I then thought maybe my last hope would be to find the container - which contains all the graphs - its height by using findViewById, but that's also not possible with RemoteViews. I then would've seen the container's height as 100% and then set the height of the other LinearLayouts according to that size. 60% would be 0.6 times the height I fetched from the container. But that, I'm afraid, is also not possible.
So I actually have no clue how to programmatically find out how high a LinearLayout is or how I can change the size of a LinearLayout percentually. It seems like I always already need to know the sizes of the containers which will be used in the widget? Which is kind of annoying as the content varies and it needs to be rendered differently based on the ratio of each gain of each asset.
Am I missing a certain way to approach this? I can't seem to find much more on the Android Developers documentation... Cheers!
The ability to affect things like that was only added in API Level 31 (Android 12), with methods like setViewLayoutHeight(). So, one option would be to set your minSdk to be 31 and live with a smaller user base for a while.
Or:
For a complex UI, you might consider rendering a Bitmap inside your app, then showing that Bitmap in an ImageView in the app widget (with the Bitmap served via a ContentProvider). This is annoying, but it gives you ultimate flexibility.
Another possibility is to have a bunch of different layout resources, where you encode a range of desired weights, the use the desired one in the RemoteViews when you go to update it. If you keep consistent widget IDs, the rest of your code should not need to change much.
You have the question tagged as java. If you eventually learn Kotlin, you could see if Glance has some more flexibility. Glance uses Jetpack Compose to create app widgets (and Wear OS tiles).
Use directly :
view.setViewLayoutHeightDimen(your layoutId, int heightDimen)

Using multiple backgrounds in Android Application

Here is a scenario on which i done a lot of research on google but hopeless. the scenario is, i am going to develop a 2D game in which i want to use multiple backgrounds and i want to translate them as they are moving forward, it must look like one background is near and translating/moving fast and the other background is a bit far and translating/moving a little slow. the nearer background have almost full intensity and the farer background have a bit low intensity. you can say they have different transparency levels. the question is, how can i get this task done or is this possible to use multiple backgrounds.
any suggestion will be appreciated.
as an example, see the image of BADLAND game bellow.
I think as far as I got your question you want to put two or more images one over another. I mean if you are trying to overlap the multiple backgrounds and asking for it yes it can be done easily.
What you need to do is to use the FrameLayout. as
"FrameLayout represents a simple layout for the user interface of
Android applications. It is usually used for displaying single Views
at a specific area on the screen or overlapping its child views."
and then you can implements the animations on them and translate them You can find different types of translation over them.
Here is all about using the frame layout frameLayout1 framelayout2 and for animations and translation here are links. link , link2 , link3

Android Confusion on supporting multiple screen sizes

Ive read Android documentation on the subject of supporting multiple screen sizes but I just cant seem to wrap my head around it. I know qualifiers need to be set in layout names and android picks the one whos qualifier is closes to the width of the phone.
Im not to worried about tablets at the moment but when I develop for my physical device everything is fine and dandy all in default layout folder: layout/main.xml
But if I test on my friends phone Samsung Galaxy S4 everything is streched in a vertical way. I just dont know where to begin really...
Is there a standard set of folders that I could implement to lay my layouts in and then edit for optimized performance?
http://developer.android.com/training/basics/supporting-devices/screens.html. It's all explained in this tutorial link.
If one layout should support all screens then,
your app theame should be similer for tabs and mobiles
never use fixed values for layouts like (ex:300dp , 15dp), all are wrap , match , fill depends on requirement,
make all the icons, images in 2 or 4 different sizes
NOTE: if you have minor changes for smaller to larger device ui, then in on create get the device height and width , make your changes (images, layout widths etc..) runtime from java
If your app is not similer from small to big devices
Use layout(mobiles) ,layout-large(7 inch tabs) layout-xlarge(10 inch tabs)
Start with device independent pixels (dp) and scalable-independent pixels for fonts (sp)
If something is stretched vertically after that, it probably means it's a background image that's trying to fill up the entire height of a layout (by the way, next time you have a problem, please do post the relevant xml code and dimensions of the image, or at least a screenshot of the problem).
In that case, just use the different size qualifiers to solve your problem (not your density qualifiers). Density qualifiers don't help for widths or heights of bitmaps that are larger than the widths or the heights of device screens. This is just a rule of thumb that you should be aware of.
Use the toolbar select box in the layout designer of Android Studio/IntelliJ to quickly test multiple device screens all in one go. This is the fastest way to do it. Do not use Eclipse for that, its ADT layout designer doesn't have that capability (at least, not the last time I've checked). Even if you use Eclipse most of the time, it's worth using Android Studio/intelliJ just for that functionality, and then switching back to Eclipse (assuming it's still your favorite IDE after that) when you're done with testing that aspect of the layout.
Also, don't try to do a different layout for each size qualifier that you encounter. Only use different size qualifiers for the layouts that are actually giving you problems. And don't forget that common layout components that don't need to be duplicated can just be abstracted away in a parent layout. And if you can find a way to resolve your specific problem without the use of size qualifiers, that can work just as well. For instance, if instead of using a single image for your background, you could replace it with a larger image that you don't mind getting clipped at different aspect ratios, or replace the background image with something entirely different and abstract (like a solid color, a gradient, a tiling background, a large vector graphic, or a large patch-nine png) that looks ok at different aspect ratios, that could be even simpler still.
At the high level, you just need to take care of these things:
- Better have the images sliced for either XHDPI (720x1280) OR XXHDPI (1080x1920) resolution and keep them in respective folders. XHDPI images into drawable-xhdpi and XXHDPI images into drawable-xxhdpi folder. One set of image slices are enough.
- You don't need to write different XML layouts for the same screen if you are not supporting Tablets (except for very rare times)
- Try to avoid hard coding the android:layout_width & android:layout_height parameters. Use wrap_content, fill_parent OR match_parent.
- Try to avoid keeping any image slices inside simple drawable folder instead keep inside specific drawable folders like drawable-xhdpi , drawable-hdpi etc.
- Try to use, color codes & 9 patch images as much as possible instead of using images for everything which will reduce the build size and also helps in avoiding OutOfMemoryException.
- Inside simple drawable folder, keep all your XML drawables like background of a button with separate images for clicked & focused states etc.
If you follow these steps, you don't need to worry about supporting multiple screen resolutions for most of the cases.

java android - fit items on a image background with different screen size

This is my first post and i have (i think) a uncommon request...
I'm programming on the last Eclipse Mac, for every android SDK, and i'm using more XML for layout than runtime... So i search a solution in XML if possible.
Here is the situation: i work with an image background wich simulate item like buttons, image, text... etc. On this image, i put buttons, image, and text (buttonview textview etc...) and place it precisely on their places on the image. This solution is very powerful to have good design BUT, if i change the resolution of the screen, and/or its size, each item won't be at its place, and will be translated (horiz and/or vertic) for some "dp"... (and yes i use dp, not mm or px or whatever)
I'm really embarrased because i think thanks to "dp" it keep proportionnality but.. not !
My question is how can i fix my items at their places on the image background, for different screen size/resolution !!
Thanks in advance everyone,
My Best From Lyon,France
First realise that if you want to make it pixel perfect for all screen sizes your out of luck.
Second accept that you can't make it perfect for all screen sizes.
Third you can get far with creating different layout for different screen sizes. You can read a lot about supporting multiple screen sizes here. One important thing to take from here is that you can make layout for the different screen sizes or different density sizes.

Best practices to use when targeting multiple screen resolutions on Android

When designing an UI, we need to target many android mutations and various screen resolutions.
How to differentiate layout for 480 * 800 and 480 * 854 screen resolutions in android?
how to layout support for various screen in android?
Both questions above recommend to use different Layout code for different screens. Is it really necessary?
http://developer.android.com/guide/practices/screens_support.html
recommends to
Provide different layouts for different screen sizes
Provide different bitmap drawables for different screen densities
But I still think, we can support multiple screens without creating standalone layout for each screen resolution. Or am I wrong?
I know, and I do use, 9-patch for scalable background images (or form inputs, and so on)
and I do use dp (density pixel) to declare sizes relatively to screen density
But what are next best practices ?
I think the answer to this has changed recently, but I'll go over historical solutions.
Pre-Honeycomb, the solution was to create a dynamic layout that could adjust itself to the size of the screen (using 9-patches, dips, layouts that expand/shrink, etc.). This meant that none of your Views could be "pixel perfect", but you could adjust to different aspect ratios that you encounter. Sometimes you would come up with different layouts for landscape and portrait, but beyond that customization wasn't necessary.
With the release of Honeycomb, the problem got a lot more complex. Now you've got dramatically different screen sizes, where your app stretching doesn't look good at all. You've got tablets that stretch from 7" to 10" - which one is "large" vs. "xlarge"? 3.0 and 3.1 were an awkward period, where you had to detect the API version/screen size and configure your app accordingly (supposing your app supports both phones and tablets).
However, everything's changed in 3.2 and beyond. Now, the best practice can be described thus: think like a web designer, not an Android designer. Instead of thinking about phone vs. tablet vs. landscape vs. portrait layouts, think instead of layouts that work on particular screen sizes.
This thought process is detailed by this blog post, and these slides, but I think it's best demonstrated by going to some sample web pages and seeing it in action. Try visiting this page (or this page, or this page) and changing the size of your browser. Notice how they dynamically change layout based on the width - this is what you want in Android as well.
So now you've got a layout that works between screen width 150dp and 400dp; another one that works between 401dp and 800dp; a third that handles 800dp and 1000dp, etc. This way of thinking does not end up with you, as a developer, doing too much more work than before - instead of defining a phone layout, a tablet layout (each with a portrait/landscape layout), you just define a few layouts that work with different widths.
Right now we're in an awkward transitional stage as most people don't have devices that support this practice. So "best practice" is essentially all of the above. In a few years, when everyone's got ICS and beyond, then we can all breath a sigh of relief.
(I apologize if you were looking for specific solutions; this answer ended up being relatively theoretical rather than having concrete answers, mostly because I felt the question was pretty open-ended. If you've got a specific problem you want to solve, I can try to address that elsewhere.)
If you build your views using "dp" it would, basically, be the same size for eack screen size.
In most cases you will prefer that your view will resize itself proportional to the screen size.
Of course, in most cases you will need to build separate layouts for tablets.
But, besides I can recommend you to do the next steps:
1. Add this library to your project.
2. Now in your layout you can write views like that:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm scalable!"
android:textSize="#dimen/_12sdp"/>
In this example your TextView will scale on each screen size.
These are some suggestions, in random order:
Avoid AbsoluteLayout, I know that it is deprecated, but you can still use it; forget it and use LinearLayout and RelativeLayout instead
Use 9-patch to create buttons and backgrounds, so that they will appear the same for each resolution
Use DisplayMetrics() to get informations about the screen, to show different things in different screens (i.e. to show AdMob "Banner" in smartphones and AdMob "Leaderboard" in tablets)
Create WVGA layout first (this is only a personal opinion, but I've found it a nice way of working): as far as I know the most smartphones are WVGA or at least HVGA (QVGA is not so frequent), so creating a perfect WVGA layout will make you able to satisfy lots of phones, than you can create a different layout for lpdi-screen and tablets (that are a market-share minority too, at the moment)
In conclusion I recommend you to create a layout for phones and on for tablets, that can adjust itself to screen resolution: this will fit the most devices; for borderline devices (i.e. 7" tablets or 5.3" phone) I'd work with different drawables/different XML layouts.
I would suggest as much as possible use Relative calculations rather than Absolute. Avoid Hard coding of numbers for padding, margins etc. Simply give them a value in relation to desired property like Width, height of screen or any component.

Categories