Mateusz Budnik
Posted on July 9, 2021
In many cases, we need to have some image next to the text. We can do that using designer view by setting e.q. drawableLeft text field property:
But this approach is sometimes not the best. We don't have full control over the drawable position and size. Of course, there is some way of changing image size like scaling the entire view, but sometimes it may make matters even worse.
An alternative way of doing that is setting compound drawable programmatically. The code below shows how to do it.
final float density = getResource().getDisplayMetrics().density;
final Drawable drawable = getResources().getDrawable(R.drawable.some_drawable);
final int width = Math.round(12 * density);
final int height = Math.round(12 * density);
drawable.setBounds(0, 0, width, height);
view.setCompoundDrawables(drawable, null, null, null);
💖 💪 🙅 🚩
Mateusz Budnik
Posted on July 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
android How to use ZEGOCLOUD SDK to implement Voice change/Reverb/Virtual stereo in Android
May 27, 2022