Android how to resize (scale) an xml vector icon programmatically

ThanosFisherman picture ThanosFisherman · Jun 28, 2016 · Viewed 9.4k times · Source

This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.

This is what I've done so far which is not working

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);

The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.

Any ideas?

Answer

Farhad Faghihi picture Farhad Faghihi · Jun 28, 2016

You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.

ImageView iv = (ImageView) findViewById(R.id.imgview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(params);