MainActivity.kt
package com.cfsuman.kotlintutorials
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the widgets reference from XML layout
val button = findViewById<Button>(R.id.button)
val ivOriginal = findViewById<ImageView>(R.id.ivOriginal)
val ivResized = findViewById<ImageView>(R.id.ivResized)
val tvOriginal = findViewById<TextView>(R.id.tvOriginal)
val tvResized = findViewById<TextView>(R.id.tvResized)
// Get the bitmap from assets and display into image view
val bitmap = assetsToBitmap("flower.jpg")
// If bitmap is not null
bitmap?.let {
ivOriginal.setImageBitmap(bitmap)
// Display the original bitmap width and height
tvOriginal.text = "${bitmap.width} * ${bitmap.height}"
}
// Click listener for button widget
button.setOnClickListener{
if(bitmap!=null){
// Resize the bitmap and display it into image view
val resizedBitmap = resizeBitmap(
bitmap = bitmap,
width = Random.nextInt(300,600),
height = Random.nextInt(200,400)
)
ivResized.setImageBitmap(resizedBitmap)
// Display the resized bitmap width and height
tvResized.text =
"${resizedBitmap.width} * ${resizedBitmap.height}"
}else{
Log.d("Error","bitmap not found.")
}
}
}
// Method to get a bitmap from assets
private fun assetsToBitmap(fileName:String):Bitmap?{
return try{
val stream = assets.open(fileName)
BitmapFactory.decodeStream(stream)
}catch (e:IOException){
e.printStackTrace()
null
}
}
// Method to resize a bitmap programmatically
private fun resizeBitmap(bitmap:Bitmap,width:Int,height:Int)
:Bitmap{
return Bitmap.createScaledBitmap(
bitmap, width, height,false
)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#F8F8F8"
android:padding="12dp">
<ImageView
android:id="@+id/ivOriginal"
android:layout_width="0dp"
android:layout_height="215dp"
android:scaleType="centerInside"
android:background="#E6E6E6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvOriginal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivOriginal" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginTop="16dp"
android:text="Resize Bitmap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvOriginal" />
<ImageView
android:id="@+id/ivResized"
android:layout_width="0dp"
android:layout_height="215dp"
android:layout_marginTop="16dp"
android:background="#E6E6E6"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/tvResized"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivResized" />
</androidx.constraintlayout.widget.ConstraintLayout>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzV3aszN5hfAkgLj_XQXOiQZ6OrxSzwDTGh8tDOo_JpS24FnxoN5vqLDk3MS0KDKJUlao4H2RRLbDXaLIbCtGQBO0BoA7YQlo8s-tJ5MazRevBdbXC417vuTLwqPcwHC6ALxsJqq8GOVCUzQHmg77tmCJLMz_IzWsvGpbGVTnqsHGtGgt025mA5s9vvQ/s1600/android_kotlin_how_to_resize_a_bitmap.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAYtB37-xfJSOvf6QGKHBnKFS24mSMAsMuVI8GUDK3oqPG5m0qau8k2xdLXgrKShEm8M5JJEmpcq0WPLuoDKDCx0ZYsYHSU_UOhoGjn5IGRKTNm_EDqdGojL5bPpLUOQm4-W9HYtfYBer2skVEAXVgrkjco0bfmjsMzzSNkNVXp6bEGZJP3TqlvzBbcw/s1600/android_kotlin_how_to_resize_a_bitmap2.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAsW1vi64r4tJXMlsJMxYkaH7_g6JeaUilAQ4J369GBu0mM_1XAr2bt8cyqPbVl2Z9iCmLETTt3CEuspY90hbnxxt0CQtHOft2RHLAjHKtU-KhwXfEvmCmbhmJN8mSiDJ2GpcaIzBc4UVaRgYUucz800Bmc_N8zRtqb-JJ6nmesG77rkeTOZosbNoukQ/s1600/android_kotlin_how_to_resize_a_bitmap3.png)