[Solved] android font picker library


Since I couldn’t find any libraries, I implemented the FontPicker myself.
Here is Jetpack Compose implementation:

mentalTextApi::class)
@Composable
fun FontPicker(
    fonts: Map<File, Font>,
    initialFontPath: String? = null,
    onFontChosen: (font: Font?) -> Unit,
) {
    var showMenu by remember { mutableStateOf(false) }
    var fontPath by remember { mutableStateOf(initialFontPath) }

    Column(
        modifier = Modifier
            .clickable { showMenu = true }
            .fillMaxWidth()
            .padding(8.dp),
        verticalArrangement = Arrangement.SpaceEvenly,
    ) {
        Text("Font:")

        if (fontPath == null) {
            Text("Default")
        } else {
            val fontFile = File(fontPath)
            Text(
                fontFile.nameWithoutExtension,
                fontFamily = FontFamily(Font(fontFile)),
            )
        }
    }

    DropdownMenu(
        expanded = showMenu,
        onDismissRequest = { showMenu = false },
    ) {

        DropdownMenuItem(
            text = { Text("Default", fontSize = 25.sp) },
            onClick = {
                showMenu = false
                fontPath = null
                onFontChosen(null)
            }
        )

        for ((file, font) in fonts) {
            DropdownMenuItem(
                text = {
                    Text(
                        file.nameWithoutExtension,
                        fontFamily = FontFamily(font),
                        fontSize = 25.sp,
                    )
                },
                onClick = {
                    showMenu = false
                    fontPath = file.path
                    onFontChosen(font)
                }
            )
        }
    }
}

Here is how to use it:

val fonts = File("/system/fonts")
            .listFiles()!!.associateWith { Font(it) }
FontPicker(fonts) {
    // use picked font here
}

You will get this:
enter image description here
And if you tap on it, a popup menu will be shown:

The popup menu might take some time to show because of all the fonts that have to be loaded, but it’s quicker on release builds.

solved android font picker library