Android Different layouts for different screen resolutions - java

Is this done by creating identically named xml files and placing each in drawable-ldpi drawable-hdpi folders? At the moment I am doing this but only different images are being used. No matter how I change the ldpi folders xml the hdpi's is used.
Am i doing something wrong? Or can I force the emulator to update (I am pushing the current apk to it) ?

The folder modifiers -ldpi, -hdpi, etc., can be used for other resource folders besides drawable. You can also have layout-ldpi, layout-hdpi, etc. Each would contain an identically named layout xml file, each with different xml (or not).
Be aware that the logic used in the emulator to decide what folder to use is rather complicated and depends on both the emulated dpi of the device and the scaling factor used for rendering the emulator screen. You may think you are emulating an ldpi device, but the emulator may still be deciding to bind the hdpi resources.

Related

Android Studio - Drawable folders

I have three general questions regarding android's drawable folders.
Do i put different sized images in each of these folders, or will the images automatically scale themselves? As in, does android decrease the quality of the .bmp files automatically if placed in these folders?
When using the android design preview screen, will the appropriate image from the appropriate drawable folder be shown? If I have to make different sized images for each folder, I want to ensure that what i'm seeing in the design preview matches what is shown on other devices.
Lastly, do the drawable folders, if used, help to avoid the issue with failing to allocate memory for drawables on devices? I have had to scale my images down, and yet my college's phone still cannot allocate enough resources.
I couldn't find answers to these specific questions anywhere, so i'd really appreciate the help with these!
1) You don't need the different folders if you will put the same things in those folders. The designer/developer puts the resources that most adapt to that configuration.
2) You can select what kind of device you're previewing the design with (resolution and dpi), on the design tools. It will attempt to load the appropriate resource for that configuration.
3) Loading smaller images into memory may be helpful, as well as resizing them before displaying them (libraries like Picasso can do this out of the box), or you could be looking at a leak of some sort.
About your first question, you should create four different drawable folders in app>res in order to provide different devices (with different screen sizes and densities) more convenient images.
/drawable-ldpi For low density screens.
/drawable-mdpi For medium density screens.
/drawable-hdpi For high resolution screens.
/drawable-xhdpi For extra high resolution screens.
Android does not decrease the quality of .bmp files when they are allocated in those folders. You have to fill each folder with the correct sized images.
About second question, I am not sure whether the android design preview screen uses the correct images or not, but in a real app running on a phone, it will.
And third question: sorry, but not.
Good luck!
Ad 1 I always find it really helpful to load drawables to my project using Android Drawable Importer plugin. It will take care of loading appropriately-sized image into appropriate drawables folder.
Ad 3 If that works for you, maybe try loading images from server using tools like Glade or Picasso? That way you don't clutter your app with unnecessary resources, making it too heavy data-wise. Use a local drawable as placeholder only, in case there's no internet connection.
I'd also recommend using .png over .bmp format, .png are lossless and compressed, meaning your images can get significantly smaller while not losing quality.
You have to put images in different folders as per the image size. it will not generate automatically.
You can not select image for that screen. you can just select different screen sizes android studio will automatically use proper images for that.
For memory issue you can add this line in manifest :
android:hardwareAccelerated="true"

Assets behaviour in multiplatform libGDX application

What is the best practice to store resources in libGDX library. I'm know that I can use AssetManager and also e.g. I can link the resources from android folder into iOS, but I dont know how it will behaviour on multiplatform devices. The resources are scale according to screen size/operating system, or I need to manually set diffrent size or resulution in each platform resource folder.I want to avoid any ovelaying or stretch behaviour.
There are many ways to go about this and there is no "best" solution. However if you do already build for android just use the android assets folder. This is the default and will be used for other builds (due to the default libgdx project configurations).
The resources only scale if you tell them too. You can choose to use a viewport (a fit/fill viewport will not stretch but can add black/background bars that do not have the default aspect ratio). But you can also choose to implement screen dependency yourself by using the aspect ratio and the scale.
For instance:
A 1080x1920 mobile phone vs a 1440x1920 tablet
If you use a fit viewport you will have unused space on the tablet. if you use a fillviewport you might lose stuff on the phone. But if you take the phone as a default aspect ratio and calculate the width offset for the tablet (1440-1080/2) you can use this value to choose to put actors/sprites on the same location as on the prone (by using this offset) or relative to the screen edge (by using the screen size). I personally use this to place the UI relative to the screen and the game itself the same as on the phone. You can even choose to use a different layout depending on the aspect ratio.
Do note that in this way you will also have to calculate a global scale and use this everywhere in your application. This can be tedious to implement but gives you much more control!
So if you have a simple game and you don't care about tablets or different screen sizes I suggest you start with a fit viewport.
p.s. Not sure what you mean by "multiplatform devices", but as I said, the default libGDX setup does the heavy lifting here, so I suggest you use it!

How to use single drawable for all screen resolutions in Android programming?

Android programming in eclipse, java, xml etc. I put all the drawables (images, xml files etc) in all the folders: mdpi, hdpi etc. So I have 4 copies of each file.
Is this absolutely necessary ?
Is there a way I can put them in a single "drawable" folder ?
Or will android understand that if the file does not exist in hdpi, it should fetch it from mdpi, or wherever the file exists ?
Yes, it will fetch the appropriate file if that particular image is
not available in other folder. But it need to atlest in one folder(drawable OR mdpi OR hdpi OR ldpi ...)
Yes you can put all the images in single drawable folder.But in this case if you have a low resolution image this will be look as faded image in the case of tabs
so if you want your application support in all devices, Tabs then you have to put images in different drawable folder like as drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi
Why not use https://code.google.com/p/9patch-resizer/ to automatically resize your images to all resolution compatible for android development?

Android drawable paradigm!

I have been developing Android application since 3 to 4 months. I am naive, But pretty much exposed to all of the fundamentals regarding application development on android. However I found really painful while developing application with lots of images, By saying images I mean one of my application has around 10 to 13 images(Small enough to accommodate screen size). The problem is I have to make different copies of it by making,
HDPI - High resolution support
MDPI - Medium resolution support
LDPI - Low resolution support
I have come up with an idea,
IDEA : My idea is to actually have only MDPI images in drawable folder, When my
application will installed first time, I want my application to detect what type of
resolution is supported by device? After knowing which resolution is supported one of my
built in method will either use a MDPI version(images), if handset supports it or else
it will scale up or scale down my images and stores into internal storage for future
reference. When user uninstall my application I will remove these images from internal
storage.
Now this idea has raised a question,
Question :
Whether this idea is feasible? and Programatically possible?
If it is, Should I be really concerned about one time computational overhead?
Is there any mechanism(third party) which can ease my problem? (I hate photoshop and scaling up and down all those images)
Any expert help or guidance will be a big favour!
Thanks in advance!
Krio
I dont really understand why you would do this. The system already basically does this for you. You dont have to specify different images for different display densities, the system just gives you the opportunity to so you can make your app look its best. If you only supply a single image the system will scale it appropriately for you based on the density of the handset.
As for help with scaling the images yourself for packaging, you could look at image magick. This is a powerful scriptable image manipulation tool. You might need to spend a bit of time getting up to speed with it, but I am sure you could write a script that you could reuse for all of your images after that to convert high dpi images to lower dpi ones by scaling down.
Take a look to this article. It describes how android handle directory names for resources. Also take a look a look to this - how android choose the best match for directory name. If you want to use the same resource for all dpis just place it in the default drawable folder.

How are PNG files defined as drawable objects resized based on the resolution of the Android phone?

I have a PNG file that is 32x32 pixels. I use 8 of these as drawables in a row for my app. These drawables are not in the hdpi, mdpi, or ldpi folders. I’ve found that when starting any of the 3 standard size emulators, the screen view with all 8 drawables looks pretty much the same.
I note that the ldpi emulator I’m using (QVGA) has a resolution of 240x320. 8 x 32 = 256, so since I can see all my drawables (and space in-between) I’m betting something is changing their size.
I’ve read the Supporting Multiple Screens document at the Android developers page, but I still don’t understand what is actually happening. Could you put your own words to explain what is happening to the size of my drawables and how the SDK knows to automatically modify them?
Of course - the system automatically resizes your images from the drawable directory to correspond to the DPI of the device if you don't define your own. So in fact the 'drawable' directory is pretty much the same as the 'drawable-mdpi' directory, and because you don't have the ldpi and hdpi versions defined the system automatically builds them out of your mdpi resources. See (1.) here: http://developer.android.com/guide/practices/screens_support.html#support - "Pre-scaling of resources (such as image assets)"
Of course your icons will look somehow ok on ldpi devices but ugly on hdpi devices. That's because the mdpi icons are scaled to match higher dimensions, effectively resulting in less information per pixel.

Categories