MainActivity.kt
package com.example.jetpack
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// convert drawable to bitmap
val bitmap:Bitmap = (ResourcesCompat.getDrawable(
resources,R.drawable.flower,null)
as BitmapDrawable).bitmap
// set image view image source bitmap
imageView.setImageBitmap(bitmap)
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - Drawable tint programmatically
- android kotlin - Set image from drawable
- android kotlin - Create GradientDrawable Programmatically
- android kotlin - ActionBar background color programmatically
- android kotlin - Change indeterminate progress bar color
- android kotlin - Change circular progress bar color
- android kotlin - Custom circular progress bar
- android kotlin - Vertical progress bar example
- android kotlin - Indeterminate ProgressBar example
- android kotlin - Horizontal ProgressBar programmatically
- android kotlin - Change horizontal ProgressBar color
- android kotlin - Horizontal ProgressBar example
- android kotlin - selectableItemBackground programmatically
- android kotlin - ListView disable item programmatically
- android kotlin - ListView item height programmatically