Asghar Nazir
Asghar Nazir

Reputation: 369

How to change the size of vector drawable programmatically using java

I want to change the size of the icons that are displayed in my recycler view. My recycler view basically shows categories of expenses. I have hardcoded some of the categories with icons in the XML layout that I have created using the android studio, but the user can also define their own categories and can select an icon for that, I am using https://github.com/maltaisn/icondialoglib/wiki dialogue to allow users to select icons of their choice while defining a new category. I save the IDs of drawable icons from the dialogue to the database and in the onBindViewHolder method of adapter class I retrieve the ID and using that ID I get the icon. If I display a vector drawable that is defined in XML, the size(48x48) of the icon is showing perfect but the size of the icons that are selected from the dialogue is smaller(Image is shared below). I have tried to change it to 48x48 but it is not working.

<ImageView
        android:id="@+id/single_column_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        ></ImageView>

vector drawable that is defined in the XML.

    <vector android:autoMirrored="true" android:height="48dp"
    android:viewportHeight="32" android:viewportWidth="32"
    android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#333332" android:pathData="M24.832,11.445C24.646,11.167 24.334,11 24,11h-1c-0.553,0 -1,0.447 -1,1v6c0,0.553 0.447,1 1,1h4c0.553,0 1,-0.447 1,-1v-1.5c0,-0.197 -0.059,-0.391 -0.168,-0.555L24.832,11.445zM27,18h-4v-6h1l3,4.5V18z"/>
    <path android:fillColor="#333332" android:pathData="M31.496,15.336l-4,-6C26.938,8.499 26.004,8 25,8h-4V6c0,-1.654 -1.346,-3 -3,-3H3C1.346,3 0,4.346 0,6v11c0,1.654 1.346,3 3,3h0v3c0,1.654 1.346,3 3,3h1.142c0.447,1.721 2,3 3.859,3c1.857,0 3.41,-1.279 3.857,-3h5.282c0.447,1.721 2,3 3.859,3c1.857,0 3.41,-1.279 3.857,-3H29c1.654,0 3,-1.346 3,-3v-6C32,16.406 31.826,15.83 31.496,15.336zM3,18c-0.552,0 -1,-0.447 -1,-1V6c0,-0.553 0.448,-1 1,-1h15c0.553,0 1,0.447 1,1v2v2v7c0,0.553 -0.447,1 -1,1H3zM11.001,27c-1.105,0 -2,-0.896 -2,-2s0.895,-2 2,-2c1.104,0 2,0.896 2,2S12.104,27 11.001,27zM24,27c-1.105,0 -2,-0.896 -2,-2s0.895,-2 2,-2c1.104,0 2,0.896 2,2S25.104,27 24,27zM30,23c0,0.553 -0.447,1 -1,1h-1.143c-0.447,-1.721 -2,-3 -3.857,-3c-1.859,0 -3.412,1.279 -3.859,3h-5.282c-0.447,-1.721 -2,-3 -3.857,-3c-1.859,0 -3.412,1.279 -3.859,3H6c-0.552,0 -1,-0.447 -1,-1v-3h13c1.654,0 3,-1.346 3,-3v-7h4c0.334,0 0.646,0.167 0.832,0.445l4,6C29.941,16.609 30,16.803 30,17V23z"/>
</vector>

Java code

@Override
public void onBindViewHolder(@NonNull CategoryViewHolder holder, int position) {

String categoryName=addCategoriesList.get(position).getColumn_value();
holder.textView.setText(categoryName);
if(categoryName.equals("Transport")){
    holder.imageView.setImageResource(R.drawable.ic_transport);
}else{
    if(IconHelper.getInstance(holder.imageView.getContext()).getIcon(addCategoriesList.get(position).getIconID())!=null){
        Drawable drawable=IconHelper.getInstance(holder.imageView.getContext()).getIcon(addCategoriesList.get(position).getIconID()).getDrawable(holder.imageView.getContext());
        drawable.setBounds(0,0,100,100);

    holder.imageView.setImageDrawable(drawable);
}}
}

enter image description here

Upvotes: 1

Views: 2209

Answers (1)

Ben P.
Ben P.

Reputation: 54224

This line looks like a likely culprit:

drawable.setBounds(0,0,100,100);

These are pixel values, and so they will cause the icon to appear at different physical sizes depending on the pixel density of the device you're using to test. If your device is xxxhdpi, 100px will be much smaller than 48dp.

You could convert px to dp in order to get the right values to use here, but I think you can go even simpler. Once you know that you're using a user-selected image, you don't actually have to build the Drawable object. Instead, you can use the resource value directly:

int iconId = addCategoriesList.get(position).getIconID();
holder.imageView.setImageResource(iconId);

Upvotes: 1

Related Questions