Get correct Laucher icon size on Android - android

I am working on an app which is able to add shortcuts icons to the homescreen.
What is the correct icon size for Android tablets? (Or better said, how to get it at runtime?) It seems to differ from what is written on this page.
I have switch on DisplayMetrics.densityDpi and change the icon size accordingly. But it does not work for all devices..Gnex seems to be ok, but my Galaxy Tab while beeing DisplayMetrics.DENSITY_MEDIUM device displays only 48x48px icon (as it is in guidelines), but usual launcher icons are 72x72 pixels and not 48x48 - it differs from guidelines? Is it possible that it is related to TouchWiz UI and thus it differs from pure Android? Or where is the problem? Also good to note, that apps itself displays correct launcher icon (it takes the icon from hdpi folder), but at runtime it looks like it is mdpi device:/
Thanks

Android tablets looks one bucket up, when looking for launcher icon..That means mdpi device will actually look into hdpi folder..
This code returns correct size for launcher icons
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int iconSize = am.getLauncherLargeIconSize();
Answer is based on this g+ post by Nick Butcher

best thing you use the android assets studio tool for this task .
it will automatically create the correct sizes and put them in the correct folders .

Related

Why Android Launcher picks a low-density icon?

I've created 2 kinds of launcher icons:
mipmap-mdpi (48x48)
mipmap-hdpi (72x72)
Now I'm checking out what kind of icon Lollipop's Launcher chooses. And it chooses the mdpi icon, and then stretches the icon up to 60x60px:
Demo:
The question is:
Why the Launcher picks mdpi instead of hdpi?
By default it takes the lower size icon.
So, if you want to use large pixel icon then give some different name.
for exp:
Use two icons of different size and give same name and put it to any extension folder like mdpi,ldpi,hdpi,xhdpi or xxhdpi. It will always prefer smallest size.
The launcher sometimes picks the mdpi icon instead of hdpi icon because of the custom theme applied on the device, or because of the custom launcher.
if you are using the default launcher by your device brand you can use some other custom launcher you will definitely be shocked to see the results

ldpi icons not showing android

I have an app that has buttons with background images that I specify in the layout. For mdpi and up it works fine and you can see the images. But I recently started tested on a small device and saw the backgrounds only show black. I then checked the screen size (small) which I cater for and also checked the density, which I saw in this case is ldpi. So I rescaled the mdpi images to 36x36 and created a mipmap-ldpi folder. Android studio sees it in design time but for some reason it is not picked up at runtime. So I Googled around and saw that I might have to add them manually via Android Studio. So I created a New Resource Directory where I specified the density as ldpi as below :
I then for each Icon added a new file with the same name and selected the ldpi folder as below :
And select ldpi directory :
I then entered the same name as it is with all the other densities for each icon / image.
After all this I still get black background so I thought let me try and assign the background programmatically. That then works. Can anyone help me with why it will not work when specifying it in the layout file. I have a layout file specifically for small screens. Must I specify density as well ?
Apologies for the images. I see I cannot embed yet because my reputation is too low.
Update 1 :
Moved all the icons to Drawables as recommended and it was still doing it.
Read other articles where LDPI is not really supported anymore and then answered the question as to make the problem go away by not supporting devices with LDPI density.
But the next day then picked the same problem up on a old S2 with HPDI density. So it is possibly not a LDPI problem anymore until proven otherwise. Hopefully we will find the problem and get it fixed.
The mipmap directories are used to store the launcher icons. They are not used inside the app, but at the home screen and the app listing.
The reason is, when a resource from drawable-directory is requested, a bitmap is chosen from the folder that matches the current density. But, when you use the mipmap drawable, the launcher may choose an icon from a different folder (usually a size-up). That's why you're not seeing the ldpi icon.
Some launchers actually display the icons larger than they were intended. Therefore, using the mipmap for launchers helps in this.
Also check this link out.
SUMMARY: use drawables for icons used inside the app and mipmap for the launcher icon of the app.
Apologies. I asked my question incorrectly. I assumed it was the icons but I neglected to mention I call the icons from Layer-List drawables. Turns out Layer-List draws black on some devices that runs Android 4.0 and 4.1 if you do not explicitly mention transparent as a color. I can now see all icons from ldpi up.
Thanks for your assistance. Reference link : Android xml layer-list not displayed correctly on some devices

Can I only set one icon in android for all the dpis?

So im creating a new app and I was wondering if i could only make one icon(technically two but one is 512*512 for the play store). So ill just make a max size icon for xxhdpi will lower res phones scale correctly, or would i actually have to make one for each dpi level. If that is the case, are there any tools that creates the different sizes for me?
You have to make one for each screen density. There are tools available to assist you in this: http://romannurik.github.io/AndroidAssetStudio/index.html
You really have to have an icon for each density value, otherwise you will have a 512*512 icon being used in a ldpi phone, which is very inefficient, as for ldpi phones you should use a 32x32 icon instead. Just use an image editor and resize your original icon or use on-line tools to resize it for you. ynnadkrap has a good example of a tool you can use to get your icons.

Android get xhdpi icon from ActivityManager.RunningTaskInfo

I'm successfully getting a recent app's icon using this code:
//app is an ActivityManager.RunningTaskInfo
//image is an ImageView
ApplicationInfo appInfo = packageManager.getApplicationInfo(app.topActivity.getPackageName(), 0);
Drawable d = packageManager.getApplicationIcon(appInfo);
image.setImageDrawable(d);
However the image displayed is only 48x48px. I'm guessing this is because my device is mdpi, however it's making the icons very blurry. Is there a way to always get the xhdpi icon, regardless of the devices display density? Thanks for any help.
I don't think you can get any other density icons out of packageManager.getApplicationIcon(appInfo); without some hacking. Either you would hack the device's reported screen DPI or somehow convince PackageManager that you have a xhdpi screen size (you can specify a Bitmap's density, but I don't know how you could pass that to PackageManager).
However there might be an easier way to get the xhdpi icon (and other DP icons as well). If you can get the application's apk to your computer and unzip it, you should find the res/drawable* folder(s) inside where you could get the icon from.
Though if you need xhdpi icons during your app runtime and for any app on the device, this sadly is not the solution to your problem. If the latter is the case, note also that not every app has xhdpi resources/icons.

Android menu icon smaller in honeycomb

I've created a menu icon and included a 36x36, 48x48 and 72x72 in the ldpi, mdpi and hdpi drawable folders. It looks fine on all devices except honeycomb tablets, where it seems like the padding around the Android icons are bigger than on my custom icon. I've included a screenshot (see how much bigger my 'Resume Reading' icon is than the android 'Back' and 'My Library' icons are). How do I make my icon look like this?
I think I found the problem. When using the Android resource icon's on a tablet, I think it changes the padding in order for the icon to be used in the action bar. So when displaying them in a pop up menu at the bottom they are appearing much smaller than they should be. The solution is to copy the android resources into our own resource folder as it says to do here http://developer.android.com/guide/practices/ui_guidelines/icon_design_menu.html
Most of your Tablets are not hdpi, they are xhdpi. While the Icon Design Guidelines are useful, they don't give the whole story. Consider reading Supporting Multiple Screens. Here, they tell you further ratios and dimensions. To save you some time, resources should be 3ldpi:4mdpi:6hdpi:8xhdpi meaning that your xhdpi icon should be 96 x 96. Please, read the whole document, though. It is insanely useful.

Categories

Resources