Skip to content

Instantly share code, notes, and snippets.

@tkuenneth
Created May 29, 2021 10:07
Show Gist options
  • Save tkuenneth/ddf598663f041dc79960cda503d14448 to your computer and use it in GitHub Desktop.
Save tkuenneth/ddf598663f041dc79960cda503d14448 to your computer and use it in GitHub Desktop.
How to display an adaptive icon in Jetpack Compose
package com.thomaskuenneth.sandbox
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.content.res.ResourcesCompat
import com.thomaskuenneth.sandbox.ui.theme.SandboxTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SandboxTheme {
Surface(color = MaterialTheme.colors.background) {
Sandbox()
}
}
}
}
}
@Composable
fun Sandbox() {
ResourcesCompat.getDrawable(
LocalContext.current.resources,
R.mipmap.ic_launcher, LocalContext.current.theme
)?.let { drawable ->
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
Column(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
// painter = painterResource(R.mipmap.ic_launcher),
bitmap = bitmap.asImageBitmap(),
"An image",
modifier = Modifier.requiredSize(96.dp)
)
Text("Hello Image")
}
}
}
@apkelly
Copy link

apkelly commented Aug 14, 2023

Anyone landing here from a Google search, I've found this code to work too

@Composable
fun adaptiveIconPainterResource(@DrawableRes id: Int): Painter {
    val res = LocalContext.current.resources
    val theme = LocalContext.current.theme

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Android O supports adaptive icons, try loading this first (even though this is least likely to be the format).
        val adaptiveIcon = ResourcesCompat.getDrawable(res, id, theme) as? AdaptiveIconDrawable
        if (adaptiveIcon != null) {
            BitmapPainter(adaptiveIcon.toBitmap().asImageBitmap())
        } else {
            // We couldn't load the drawable as an Adaptive Icon, just use painterResource
            painterResource(id)
        }
    } else {
        // We're not on Android O or later, just use painterResource
        painterResource(id)
    }
}

@tkuenneth
Copy link
Author

Nice one 😍🎉

@rafaelekol
Copy link

Thanks a lot

@ramonrabello
Copy link

Thanks @tkuenneth @apkelly for providing these workarounds. Both versions worked perfectly! 👏 🎉

@ALizarazoTellez
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment