0

I'd like to make icons configurable. Is it possible to access the Material Icon library by name, rather than as extension properties on untyped objects? For example, something like this:

val icon = getMaterialIcon("Filled.Crop169") // would resolve to Icons.Filled.Crop169

I'd like to avoid writing a huge when, especially since I'd have to do one for each icon type (Filled, Outlined, etc). For the record, these icons do already know what their name is. For example, Icons.Filled.Crop169 is defined as materialIcon(name = "Filled.Crop169") { ... }. I just need a way to access them by that name.

I'm using Compose Desktop, but the answer is probably the same for Android with Jetpack Compose.

Jorn
  • 20,612
  • 18
  • 79
  • 126

1 Answers1

0

See Dynamic Icon value selector in Jetpack Compose for one approach (using reflection).

Beware that if you use minifyEnabled = true when building your (release) app, the icons may be stripped and be unavailable at runtime.

To get around this, you'd need to add keep rules, which would either mean specifying all icons as keep rules, or a pattern that would possibly include more icons than you would like.

Scott Stanchfield
  • 29,742
  • 9
  • 47
  • 65