Using SFSymbols in Jetpack Compose
Tags: android, kotlin, ui, mobile
Published: 2023-10-09 14:07:09

## Motivation

When working with startups that primarily target USA customers it's very common to first develop an iOS app and then later create an Android clone.

Sometimes, you are faced with a challenge:  
**→** Develop an Android app with iOS design and backend developed with that in mind.

Almost every iOS app on the market relies on [SF Symbols](https://developer.apple.com/sf-symbols/), Apple's default icon pack similar to [Google Material icons](https://fonts.google.com/icons) or [Font Awesome icons](https://fontawesome.com/icons).

I will show a very elegant way to handle SF Symbols using Jetpack Compose.

Let's begin!

## 1\. Convert SF Symbols to drawables

Since Apple will not provide you with the icons you need to use SF Symbols on other platforms, you have to look elsewhere.

I've stumbled upon this public [Figma file](https://www.figma.com/file/x4POLyT7uWoiBxPVzQqYxA/SF-Symbols-5.0---5296-SVG-Icons-(Community)?type=design&node-id=2207-38185&mode=design&t=c4BSOcKOJimfM12D-0) with almost every SF Symbol you will ever need.

Find the symbols you need, and export them as an SVG.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYTkzeGs3aW83MjUwYTBwcjJxNGlveGw1bW05Mzc4bnd2emV4b2dqOSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ENOCq17suHEQuzYnvr/giphy.gif">

Use the SVG to create a vector asset using Android Studio.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExcGVxM2NhNjV4YnJ2MzRsa3Bvc3Z3emM3a3JwNzFlbzYxOWVkdm5xNiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/UpkkhlZewVlQWPUrTj/giphy.gif">

## 2\. Create a custom `Painter`

SF Symbols are usually resolved from strings in iOS, so we will do the same. Create `SFSymbolUtil` with a method that returns an appropriate drawable.

```kotlin
private object SFSymbolUtil {
    fun getDrawableFor(symbolName: String): Int? {
        return when (symbolName) {
            "party.popper.fill" -> R.drawable.ic_party_popper_fill_colored
            "checkmark.rectangle.fill" -> R.drawable.ic_checkmark_rectangle_fill
            "figure.run.square.stack.fill" -> R.drawable.ic_figure_run_square_stack_fill_colored
            else -> {
                Timber.e("Could resolve SFSymbol: $symbolName")
                null
            }
        }
    }
}
```

Now we can use that util to create a custom painter for SF Symbols.

```kotlin
@Composable
fun painterSFSymbol(sfSymbolName: String): Painter {
    SFSymbolUtil.getDrawableFor(symbolName = sfSymbolName)?.let {
        return painterResource(id = it)
    }
    return painterResource(id = R.drawable.ic_empty)
}
```

If the painter receives an SF Symbol for which we don't have a drawable, we can use an empty drawable to draw nothing on the screen.

```xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="1dp"
    android:height="1dp"
    android:viewportWidth="1"
    android:viewportHeight="1">

    <path
        android:pathData="M0,0Z"
        android:fillColor="#ffffff"/>

</vector>
```

That's it, you can now easily use this `Painter` with your `Image` composable.

```kotlin
    Image(
        painter = painterSFSymbol(sfSymbolName = "checkmark.rectangle.fill")),
        contentDescription = null,
        modifier = Modifier.size(100.dp),
        contentScale = ContentScale.Fit,
    )
```

If you have enjoyed or found this helpful, make sure to follow me for more content like this!

Read this on my [website]( https://dinkomarinac.me/using-sfsymbols-in-jetpack-compose).