Roony
Roony

Reputation: 113

How to use a drawable resource as a background color?

I have a drawable resource and I want to use it as a background color for my lazy column items.

Code:

    @Composable
fun HabitItem(
    habit: Habit,
    onDeleteClick: () -> Unit,
    navController: NavHostController
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
             .background(//I want to place the drawable resource here)
    ) {

Any help?

Note: The purpose of using drawable resource instead of a color is that I want to add corner radius to the items. So if there is a way to add a corner radius without drawable that will work as well.

Upvotes: 2

Views: 1077

Answers (2)

Alessandro Vendramini
Alessandro Vendramini

Reputation: 31

You can replace .background with .paint inside your Row

Something like that:

Row(
    modifier = Modifier.fillMaxWidth()
        .padding(8.dp)
        .paint(
            painter = painterResource(
                id = R.drawable.ic_name
            )
         )
) {}

Upvotes: 1

Code Poet
Code Poet

Reputation: 7995

For corner radius just use:

    Modifier.clip(shape = RoundedCornerShape(8.dp))
            .background(color)

Do the clipping before the background.

More here.

Upvotes: 2

Related Questions