I'm currently creating a menu that lets users select an AppCompatImageView
in a GridLayout
and will show that the view is selected by placing a checkmark icon in the foreground of the image. In api 23, I can do this by simply calling View.setForeground()
but in lower api's, that method doesn't exist. Is there some sort of workaround that I could use?
I've tried using a LayerDrawable
but the checkmark icon gets squished if the image I set using android:src=
isn't the same height. Here's what the code for that looks like right now:
AppCompatImageView imageView = (AppCompatImageView) view;
int id = getResources().getIdentifier("drawable/" + view.getTag(), null, getPackageName());
Drawable backgroundLayer = ContextCompat.getDrawable(this, id);
Drawable foregroundLayer = ContextCompat.getDrawable(this, R.drawable.ic_selected);
Drawable[] layers = {backgroundLayer, foregroundLayer};
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);
And here's what that ends up looking like:
When I'd like the check to be a square aspect ratio. I'd like to avoid redoing the drawable background to make it a square aspect ratio because I have a large amount of other drawables that I'd have to redo as well.
Have you tried setting the image as the background and the checkmark as the src? Once the ImageView is set to a fixed size to match the aspect ratio of the image it should work fine.
imageView.setBackgroundResource(id)
imageView.setImageResource(R.drawable.ic_selected)
Using a scaleType of CENTER if the size of the check mark is ideal or CENTER_INSIDE with padding should get you what you want.