Lorenzo Benevento
Lorenzo Benevento

Reputation: 628

Custom icons in Compose

I have a data class used to dynamically define a menu.

data class ActionItemSpec(
    val contentDescription: String,
    val icon: ImageVector,
    val onClick: () -> Unit
)

My problem is this works perfectly when using standards Icons but this class problem is it can't take anything else, like a custom drawable.

How do you create an ImageVector from a custom drawable? Can you in Compose?

Upvotes: 3

Views: 2701

Answers (1)

Pesa
Pesa

Reputation: 341

Yes, for custom icon you can call method vectorResource() on ImageVector object and pass there id of your vectorAsset.

ImageVector.vectorResource(id = R.drawable.myVectorAsset)

I had a similar case where I needed to create a custom icon for bottom navigation bar, and this is how I made it:

data class BottomNavItem(
    val name: String,
    val route: String,
    val icon: ImageVector
)

BottomNavItem(
    name = getString(R.string.destination_home),
    route = home,
    icon = ImageVector.vectorResource(id = R.drawable.ic_my_home_icon))

Upvotes: 7

Related Questions