Android Set ImageView Aspect Ratio and Load Image With Glide (Kotlin)

NOTE: android-ktx is used in this example.


The following example will apply 16:9 aspect ratio to ImageView and load the image using Glide.

// make sure we get correct width (after render)imageView.doOnLayout {        // update height based on 16:9 ratio    it.updateLayoutParams {        height = (it.width * 9/16f).toInt()    }    // load image when new height are rendered    it.doOnLayout {        // glide will follow imageview's width, height and scaleType        Glide.with(this)                .load(R.drawable.sample_image)                .into(imageView)    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.