Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

Android Jetpack Compose Icons doesn't contain some of the material icons

There're many oft-used material icons in androidx.compose.material.icons.Icons but some are missing. Just as an example there is no print icon.

...

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu  // ok
import androidx.compose.material.icons.filled.Print // error, unresolved reference

@Composable
fun IconsExample() {
    Icon(Icons.Filled.Menu)  // ok
    Icon(Icons.Filled.Print) // error, unresolved reference
}

What is the simplest way to use those missing icons in an app?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a separate dependency material-icons-extended which contains the full list of material icons, just add it into your app's build.gradle

dependencies {
  ...
  implementation "androidx.compose.material:material-icons-extended:$compose_version"
}

Now you can use any material icon, for example:

...

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu  // ok
import androidx.compose.material.icons.filled.Print // ok

@Composable
fun IconsExample() {
    Icon(Icons.Filled.Menu)  // ok
    Icon(Icons.Filled.Print) // ok
}

A note about the artifact size: Since the artifact contains all material icons for multiple themes, it's a pretty big dependency, 18MB aar as of 1.0.0-alpha10. There's a note on maven repository that recommends not to use it directly:

This module contains all Material icons. It is a very large dependency and should not be included directly.

Сonsidering that most Android projects enable code shrinking for release builds, such a large dependency won't affect the release build size but it can affect your debug build and device upload time, though I'm not sure that the influence would be significant. Actually many of compose samples use this dependency.

If only a few additional icons are required and you decided not to use material-icons-extended artifact, the icons can be easily added into your project resources using Android Studio. You can use such resource-icons like this:

...

import com.mycompany.myproject.R
import androidx.compose.ui.res.vectorResource

@Composable
fun ResourceIconExample() {
    Icon(vectorResource(R.drawable.ic_baseline_print_24))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...