Set Current WallPaper As Application's Background in JetpackCompose - android

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 = ""
)

Related

Kotlin Emulator does not run app when as use a png image as drawable

I'm using Android Studio Dolphin | 2021.3.1 and Pixel 5 API emulator.
The following code is used to show image in a button.
But when I use calc.png emulator says launch is successful but quit from application.
If I use xml image as source it works fine.
Icon(imageVector = ImageVector.vectorResource(
id = R.drawable.calc),
modifier = Modifier.size(buttonWidth),
contentDescription = "drawable icons",
tint = Color.Unspecified
)
When painter has been used it is worked.
Icon(
painter = painterResource(R.drawable.calc),
modifier = Modifier.size(buttonWidth),
contentDescription = "drawable icons",
tint = Color.Unspecified
)

share image of compose component

i want to share image of my component , but cannot accses view of it, to share.
how i can accses view of my component or share image of it.
my component is a card
#Composable
fun CardItem(modifier: Modifier=Modifier,card: Card, onClick:()->Unit) {
Box(
modifier
.background(
if (card.card_number == AllCardsActivity.NOCARD) MaterialTheme.colorScheme.secondary
else MaterialTheme.colorScheme.primary,
RoundedCornerShape(20.dp)
)
.width(CardWidth)
) {
Column(Modifier.padding(25.dp)) {....
i want to share image of my component
If you mean that you want to capture a Bitmap of a composable for the purposes of sharing it, there are at least three libraries for this:
https://github.com/PatilShreyas/Capturable
https://github.com/KaustubhPatange/kapture
https://github.com/SmartToolFactory/Compose-Screenshot
And all three are open source, so if you do not like them for some reason, you can examine their implementations, contribute back changes, etc.

Android jetpack compose - Drawable colors

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
}
}

Coil don't load image in emulator with Jetpack Compose

I need to show an image in my app by url using Coil, but this image don't load. I follow the official documentation https://coil-kt.github.io/coil/compose/.
profile card
implementation "io.coil-kt:coil-compose:1.3.1"
#Composable
fun ProfilePicture(profilePicture: String, online: Boolean) {
Card(
shape = CircleShape,
border = BorderStroke(
width = 2.dp,
color = if (online) MaterialTheme.colors.lightGreen else Color.Red
),
modifier = Modifier.padding(16.dp),
elevation = 4.dp
) {
Image(
painter = rememberImagePainter(
data = profilePicture,
builder = {
transformations(CircleCropTransformation())
}
),
modifier = Modifier.size(72.dp),
contentDescription = "Profile picture"
)
}
}
Update
An exemplo to UserModel
UserModel(
name = "John Doe",
profilePicture = "https://randomuser.me/api/portraits/men/32.jpg",
online = true
)
Coil doesn't load images on the emulator because you need to enable clear text traffic, add this line to the application tag in AndroidManifest.xml.
android:usesCleartextTraffic="true"
Then uninstall the application from your emulator and install it again, it will work.
Check your date/time on the emulator.
The problem might be caused by the fact that Android emulator seems to not synchronize date and time with the network. This makes the emulator certificate appear as expired and leads to the server refusing connection.
After setting the emulator time/date manually to the current one, downloading images started working for me.
Also cold booting the emulator might help (looks like booting from a saved image for some reason sets the date/time to the one from when the image was saved).
I had the same issue, only occurring on the emulator.
Turning off mobile data, while leaving Wi-Fi enabled solved the problem for me.

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