build dynamic android layouts - java

am trying to build android app with a dynamic layout to support multiple screens
am thinking about build all my app for a certain device with a fixed dimensions like Motorola Droid , say that the button will be 50 width 50 height
and after i finished all the app , i will reedit the sizes of elements to be a ratio betweetn the right place of them and the dimensions of the new device
also Drawables will be Scalable Drawables and i will use only the Drawables folder and remove others :
- drawable-hdpi
- drawable-ldpi
- drawable-mdpi
so i will have only one Drawables folder and only one layout xml file for every activity
and most of my layouts will be hard coded using java
so the question is : is it a true method for development ?!
may be you will ask , why ?
so my answer is , as i think my method is easer than using :
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density

The simplest (and probably the best) way to create layouts that support all screen sizes is to use a RelativeLayout.

AFAIK you can manage resoution variation of different devices by keeping the images in drawable-hdpi,drawable-mdpi,drawable-ldpi folder.
then screen sizes by providing different layout for different screen size categories by specifying layout-small,layout-large,layout-xlarge.
also you can increase number of devices supported by using nine patch and relative layout check this google official documentation regarding this topic.
hope this help.
thanks.

Related

Android S8+ warning message "does not support the current Display size setting and may behave unexpectedly"

I have this warning message in Samsung S8+ Android 7:
APP_NAME does not support the current Display size setting and may
behave unexpectedly.
What it means and how can I remove it?
Thank you
Solved by adding in supports-screens
android:xlargeScreens="true"
and removing:
android:requiresSmallestWidthDp="600"
So you need to create different folders and maintain all xml in those folders.
The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for medium, high, and extra high density screens.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
For more info see this link
https://developer.android.com/guide/practices/screens_support.html
and for S8+ u can Extra large

Create a custom layout size?

Some info
I'm trying to optimize my app for a 5.1" device, actually i have a layout and a large layout ( optimized for a 7.0" ) but if i try to put as device a device with 5.1" instead of changing the size of the main layout it open and change the large layout size...
Question?
So the question is, how can i create a layout with a custom size? I would create a layout-normall if it's possible and set it to a 5.1" because also the main layout is optimized for a 5.0"...
PS: (i'm yet using ConstraintLayout and it's not working on switching device )
pic releated :
It sounds like you are using the Screen size resource qualifiers to supply your alternate layouts:
/res/layout/mylayout.xml
/res/layout-large/mylayout.xml
I recommend that you stop doing this, and never use screen size resource qualifiers in the future. As you've noticed, the difference between "normal" and "large" is unclear, and plenty of phones count as "large".
You're much better off using Smallest width resource qualifiers:
/res/layout/mylayout.xml
/res/layout-sw360dp/mylayout.xml
/res/layout-sw600dp/mylayout.xml
Providing your alternate resources in this way gives you precise control over when a particular layout is used. Note that you can use any number here; sw487dp is totally valid.
You can read more about it here: https://developer.android.com/guide/topics/resources/providing-resources.html
The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI. Thus, the value you use should be the actual smallest dimension required by your layout (usually, this value is the "smallest width" that your layout supports, regardless of the screen's current orientation).
Some values you might use here for common screen sizes:
320, for devices with screen configurations such as:
240x320 ldpi (QVGA handset),
320x480 mdpi (handset),
480x800 hdpi (high-density handset)
480, for screens such as 480x800 mdpi (tablet/handset).
600, for screens such as 600x1024 mdpi (7" tablet).
720, for screens such as 720x1280 mdpi (10" tablet).
Even though I don't know why you would want to, but if you did want to make this app to fit only one screen size, you can use the AVD manager. Go to Tools --> Android --> AVD Manager. Then create a new virtual device with the dimensions you want.
You don't have to use this virtual device, but if you create a new one with the dimensions you want, it will appear under the drop down you have shown in your first picture at the very bottom of the spinner.

Android Game using drawable folders how?

I'm new at Android Game Development I already read (Dummies Android Game..., Sams Teach..).
I want to draw now several images (tiles) to create my map, but I also want that they are automatically scaled to the device screen as good as possible. At the moment I just know how to use images from folder drawable but they are not scaled to the screen...
I already saw some examples where they use xml files to get to images over R.id.... but for this I must use the xml as layout in the code and can't use something like this or?
//create the view object
view = new SurfaceView(this);
setContentView(view);
view.setOnTouchListener(this);
I'm a little bit confused... :(
How do other Game Programmer scale their images to the different device screens?
I would be very thankful for some examples!!!
You could let android scale the images for you automatically, but of course, the quality might not turn out the way you want, but it is less work on your part. If you want more control over the scaling of your images, the only choice is to manually create them and put them in their respective folders including the default folder (drawable folder without subfix), drawble-mdpi is for medium Dot-Per-Inch devices, such devices as 320x480 screen.
drawable-hdpi is the drawable folder for devices with a high density screen size such as 480x800 or 480x854 depending on the screen sizes as well. I am going to give you the threshold by which you use to tell if it is mdpi or hdpi so on
1.mdpi: 160dpi (dpi = Dot Per Inch, it is different from dp or dip which stands for density-independent pixel)
hdpi: 240dpi
xhpdi: 320dpi = 2 times mdpi, this includes devices such as galaxy s4 with a screen resolution of 1080x1900 depending on the screen sizes as well, you kinda get the idea of how big xhdpi is.
xxhpdi: 480dpi, this one is newer but you can be guaranteed xhdpi images will scale up to fit xxhdpi very nicely, generally, you dont need to create graphics assets for xxhdpi, xhdpi is good enough.
One rule you should remember is that, you should create images for xhdpi devices and scale down accordingly, not the other way around.
As for R.id.., the part after the "R" is called the resource type you want to reference, if it an id you put R.id, drawable use R.drawable, as in the setContentView() method, you have R.layout because you are referencing a layout resource. I hope this helps you a little
When you use xml for drawables these are located to the drawable/ folder and not the layout/ folder.
In order to use them you call R.drawable.name_of_your_drawable not R.id.... nor R.layout.....
Another way to avoid blurring in larger screens is to provide different resources for different screen dpis for this option you have to place your drawables to
drawable-mdpi/
drawable-hdpi/
drawable-xhdpi/
etc
Note: that when you use multiple folders each drawable must have the exact name in all folders and the system decides which to pick.

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.

drawable sizes for different screen sizes in android

i have a problem in android development that bored me. my problem is screen size and dealing with that. specially i have some problems with images. for example i want to create a background image for my activity that i created in photoshop and my background image contains a "HELLO" word on it. but when i put it on drawable-xhdpi folder, it seems blurry and its not sharp!! my phone is a nexus 4 and according to Google documentation i create background image in 640 x 480 size.
when i create background image in 960 x 720 size it seems better but not perfect. in this case my image file size is very high!
but what is the standard way for this? please help me to solve this problem for ever. i read google documentation but its not solve my problem!
http://developer.android.com/guide/practices/screens_support.html
You should usually avoid creating images for certain screen sizes to make them background, because there are thousands of different devices and you would have to create dozens of such images.
The first thing you need to be aware of is screen density.
Generally you create 3 to 5 images when not even looking at screen size: low (120 dpi), medium (160 dpi), high (240 dpi), extra high (320 dpi) and 2*extra high (480 dpi). These go into drawable-Xdpi folders, where X is one of l, m, h, xh, xxh.
Next thing when you want to have bigger images on bigger screens (bigger phones, small and big tablets), you may want to put images to folders like drawable-sw600dp-Xdpi. This is not a case for your phone.
Nexus 4 is a xhdpi 640x384 dp device, but you should not treat it differently than Samsung Galaxy S2 (hdpi 533x320 dp).
Create an image of smaller size for both phones and center it horizontally. E.g. 320x100 px for mdpi, 480x150 px for hdpi and 640x200 px for xhdpi (your phone).
the screen resolution for Nexus is 1280x768 (http://www.google.com/nexus/4/specs/), resize the image to this resolution. In especial consideration some images can't handle the resolution and the image became disproportionately.
for interesting
resolution calculator:
http://members.ping.de/~sven/dpi.html
This is problem of Android Fragmentation and you just cannot deal with it perfectly as there is a several hundreds different devices. As colleague above wrote Nexus 4 has resolution -1280 x 768 so for sure res of image as equal as 960 x 720 is good choice. I'm even surprised that google suggest 640 x 480 for xhdpi, it's definitely too less.
So as I said you are not able to make perfect looking graphics for all existing devices. You should choose the most popular devices from every screen category(xhdpi,mdpi,ldpi ... etc) to cover the most important market share.
With 1600+ android models even after they are categorized in few Screen size and a few DPI's its very difficult to manage layouts.. i suggest that you just concentrate on designing layouts w.r.t to screen size and then create views as Resizeable Views to neglect density effects.
Once you have created your layouts Resize the Views .. You can create a Custom View or resize on its onMeasure();

Categories