Android jetpack compose - Drawable colors - android

I am having the following issue. I am adding some vector drawable images in my project which is built using Jetpack compose. I am overriding the colors of the drawable by setting
android:fillColor="?colorPrimary"
But, the previous solution, even though it works on a usual Android project, when working on Jetpack compose it is not.
Of course, I have initialized a Material Theme with my colors/typo/shapes.
Any suggetions?
Best regards!

I've run into similar issues where compose doesn't play well with attributes. It prefers to access via colorResource(R.color.colorName).
What you might be looking for is either of the below implementations:
Icon(
Icons.Filled,
"contentDescription",
tint = MaterialTheme.colors.secondary
)
Image(
painter = painterResource(R.drawable.resourceName),
contentDescription = "contentDescription",
colorFilter = ColorFilter.tint(Color.myColor)
)
UPDATE:
I actually found something similar with font attributes. You might want to try something like this:
fun getFontFromAttribute(resId: Int, context: Context): Typeface? =
context.obtainStyledAttributes(R.style.MYSTYLE, intArrayOf(resId))
.use { array ->
val resource = array.getResourceId(array.getIndex(0), 0)
if (resource == 0) {
null
} else {
ResourcesCompat.getFont(context, resource) ?: Typeface.SANS_SERIF
}
}

Related

How to "initialize" Material 3 theme design in Android Studio

I'm sure this is a newbie question, but I'm trying to implement a Material 3 theme. I have added the themes.kt & colors.kt to my com.my.project.ui.theme package. But I don't understand what I can do next to implement these colors as my app is not using these colors and is still using the default material 3 colors that came when I started the app. I'm not finding the disconnect.
I have added the dependencies and removed the old Material dependency
//implementation 'androidx.compose.material:material:1.3.1'
implementation 'androidx.compose.material3:material3:1.1.0-alpha03'
implementation "androidx.compose.material3:material3-window-size-class:1.1.0-alpha03"
implementation "com.google.accompanist:accompanist-flowlayout:0.24.8-beta"
One thing I notice is the function in themes.kt is saying it is never used (grayed out).
#Composable
fun DzoicTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: #Composable () -> Unit) {
val useDynamicColors = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
val colors = when {
useDynamicColors && darkTheme -> dynamicDarkColorScheme(LocalContext.current)
useDynamicColors && !darkTheme -> dynamicLightColorScheme(LocalContext.current)
darkTheme -> DarkColors
else -> LightColors
}
MaterialTheme(
colorScheme = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
Where does this function get called (DzoicTheme)? Default was AppTheme but instructions said to change it. Is the name important here? Most tutorials show utilizing material 3 colors from the activity compose functions but I do all my design in xml layout files, does this matter? Any help here would be appreciated!

Set Current WallPaper As Application's Background in JetpackCompose

I have a project in JetPack compose and I was wondering if there was a method of retrieving the current wallpaper on an android device and using it as the app's background. I am able to achieve this in XML using WallpaperManager but I cant seem to make it work in compose mainly because I cant pass a drawable to the Image composable. Any Ideas would be appreciated .
//this returns a drawable
val wallpaper = WallpaperManager.getInstance(LocalContext.current).drawable
How do I use it in compose to set background image
This code worked for me
val wallpaperDrawable = WallpaperManager.getInstance(LocalContext.current).drawable
val bitmap = (wallpaperDrawable as BitmapDrawable).bitmap
Image(
modifier = Modifier.fillMaxSize(),
bitmap = bitmap.asImageBitmap(),
contentDescription = ""
)

Android Jetpack Compose (Composable) Disable Hardware Acceleration for particular composable

I have a composable, that is using NativeCanvas together with BlurMaskFilter for applying some custom blur effect. This is working fine for device with with API > 23, and the problem is caused by hardware acceleration for older devices.
Here you can see the blur is working on API 30 (left) and not so much on API 22 (right).
I know that I can disable it manually for the whole application or activity from the manifest file using android:hardwareAccelerated="false".
For Views we could disable it for particular views rather than the whole activity using view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
Is there a way we could disable the hardware acceleration for particular Composable and not for the whole application/activity?
Using Interoperability APIs you can embed ComposeView with the needed configuration inside your composable:
#Composable
fun SoftwareLayerComposable(
modifier: Modifier = Modifier,
content: #Composable () -> Unit,
) {
AndroidView(
factory = { context ->
ComposeView(context).apply {
setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
},
update = { composeView ->
composeView.setContent(content)
},
modifier = modifier,
)
}
Usage:
SoftwareLayerComposable(Modifier) {
// your view
}

Unresolved reference for Modifier.preferredSize and Modifier.prefferredHeight [closed]

I am following this YouTube Tutorial where they are using Modifier.preferredSize() on a box and Modifier.preferredHeight() on a Spacer Composable - all other chained Modifiers are fine.
However Android Studio is not recognizing these 2 options.
Here is the code that I am working with:
Column() {
var isBlue by remember { mutableStateOf(false) }
val color = if(isBlue) Color.Blue else Color.Green
Button(onClick = { isBlue = !isBlue }) {}
Spacer(modifier = Modifier.preferredHeight(128.dp))
Box(modifier = Modifier.preferredHeight(128.dp).background(color = color)){}
}
The Editor is high-lighting preferredHeight as unresolved.
This is the image from the IDE for perspective.
I am using compose_version = '1.0.1' and I'm on AS Arctic Fox
preferredSize was renamed to size and preferredHeight to height
If I face some old video/article with invalid api, I'm searching through compose-samples(official samples from the maintainers) commits to find place where this method was deprecated/renamed, it's the easiest way to know if it just was renamed or I need to change more logic. In this case change was on this commit
Modifier.preferredWidth/preferredHeight/preferredSize were renamed to width/height/size starting from 1.0.0-beta01
I resolved it by following the mouse over suggestion, pressing the alt+Enter to import automatically the required referenced packages
I am following the lessons for andoid and kotlin and had stacked with this error.

Set Drawable instance to Image in Jetpack Compose

In Jetpack Compose, who can tell me is there a way to assign a Drawable Object to the Image compose view?
I took the apps installed on an Android device. I get an icon with the type that is Drawable and I want to use it in Image
val icon: Drawable = packageInfor.applicationInfo.loadIcon(packageManager)
I found there are 3 functions that can assign images
From Painter (IdRes)
From ImageBitmap
From ImageVector
From all that I don't know how to assign a Drawable instance.
After all, I found a simple solution with Accompanist. Thanks to #Sinner of the System for suggesting to me.
Adding this dependency to the app gradle.
implementation "com.google.accompanist:accompanist-drawablepainter:<version>" //0.28.0
Using:
Image(
painter = rememberDrawablePainter(drawable = drawable),
contentDescription = "content description",
)
Check out: Drawable PainterĀ¶
Edit: You should definitely use the accompanist library as recommended in the Wilson Tran answer. Since it provides support for several kinds of drawables.
If you're not using the accompanist library, you can do the following...
ContextCompat.getDrawable(LocalContext.current, R.mipmap.ic_launcher)?.let {
Image(bitmap = it.toBitmap().asImageBitmap(), contentDescription = null)
}
You can set the drawable size in toBitmap function if you want...
If you're using the Coil library, rememberImagePainter will accept an Any? as its data argument, which includes support for Drawable instances. I'm using this as an all-in-one solution for my images rather than importing Accompanist.
Image(
painter = rememberImagePainter(data = myDrawableInstance)
)
You can do like that
val backgroundImage = painterResource(R.drawable.your_image)
and then pass this to your image like that
Image(painter = backgroundImage, contentDescription = null)
That will work.

Categories

Resources