https://kotlinlang.org logo
#glance
Title
# glance
k

Kimon

05/06/2022, 10:28 AM
Hi! Is it possible to do the following in Glance composables? • add elevation (e.g. in a Row)? Like you can in a LinearLayout? • tint a drawable
p

Piotr Prus

05/07/2022, 3:00 PM
1. IDK 2. Unfortunately not, but as a work around I make bitmap and color it accordingly
k

Kimon

05/10/2022, 8:00 AM
Thank you for your answer 🙂 When you say bitmap, you mean something like this and having the color applied to the drawable itself?
Copy code
modifier = GlanceModifier.background(imageProvider = ImageProvider(R.drawable.colored_drawable)),
p

Piotr Prus

05/11/2022, 7:43 AM
I actually create the bitmap with different color every time the color changes.
Copy code
fun createColorArc(context: Context, colorString: String): Bitmap {
        val density = context.resources.displayMetrics.density
        val bitmap = Bitmap.createBitmap(
            36f.times(density).toInt(),
            36f.times(density).toInt(),
            Bitmap.Config.ARGB_8888
        )
        val paintWidth = 3f.times(density)
        val paint = Paint().apply {
            isAntiAlias = true
            color = Color.parseColor(colorString)
            strokeWidth = paintWidth
            style = Paint.Style.STROKE
            strokeCap = Paint.Cap.ROUND
        }
        val canvas = Canvas(bitmap)
        canvas.drawArc(
            paintWidth,
            paintWidth,
            canvas.width.toFloat().minus(paintWidth),
            canvas.height.toFloat().minus(paintWidth),
            45f,
            250f,
            false,
            paint
        )
        return bitmap
    }
🙌 1
calling this from glance composable:
Copy code
Image(
                modifier = GlanceModifier.size(36.dp),
                provider = ImageProvider(
                    bitmap = WidgetUiHelper.createColorArc(
                        context,
                        data.colorHex
                    )
                ),
                contentDescription = "Color arc"
            )
p

Pierre Barbier de Reuille

05/11/2022, 8:22 AM
For your first question, sadly,
setElevation
has only been made available to
RemoteViews
on Android 12. Prior to this, app widgets can only set their elevation in XML.
👍 1
Allowing changing the elevation would therefore only work on Android 12+ for Glance.
k

Kimon

05/13/2022, 2:07 PM
I see.. Thank you for the clarification 🙂 So, is the same true with
setBackgroundTintList
for tinting background drawables?
5 Views