Ilya Pafnutov
Ilya Pafnutov

Reputation: 1

AppCompat Views don't scale drawable correctly

I'm building an android app on java using AppCompat for UI. My app has to show names and icons of apps installed on a phone in a grid, for this I use a RecycleView (VerticalGridView). I get icons using this java code:

private AppInfo[] loadAppsInfo() {
        AppInfo[] appInfo;

        PackageManager pm = getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> allApps = pm.queryIntentActivities(intent, 0);

        final int count = allApps.size();

        appInfo = new AppInfo[count];

        for (int i = 0; i < count; i++) {
            ResolveInfo     ri = allApps.get(i);
            ApplicationInfo ai = ri.activityInfo.applicationInfo;

            appInfo[i] = new AppInfo(
                ai.loadLabel(pm).toString(),
                ai.packageName             ,
                ai.loadIcon(pm)
            );
        }

        Arrays.sort(appInfo);
    
        return appInfo;
    }

My problem is that when I use any of AppCompat Views (AppCompatImageView, AppCompatImageButton, even Button with setCompoundDrawablesWithIntrinsicBounds()) they all act exactly the same, completely failing to fit and scale a Drawable resulting in this:

screenshot1
screenshot2

Calling Drawable.setBounds() to change icon size doesn't have any effect. Drawing icon on a bitmap and then setting this icon as background works, but it will take more time and memory, force me to manually calculate the size of the image in advance when drawing or scaling, and just add more complexety in my code when I sould be able to unstead just change some parameters.

When I use ImageView it works perfectly fine, but it makes my code more complex and heavy, and can cause issues when using vector images on older android versions.

Any suggesions? Thanks in advance

Upvotes: 0

Views: 34

Answers (0)

Related Questions