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.
Related
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 = ""
)
Good Morning, I'm developing a new Wear OS application. The thing is, I can't find anywhere how to make a good pagination.
I would like to change activity on swipe (no matter if it's vertical or horizontal) but can't find anywhere how to do it...
Maybe I should not navigate in my app like that ?
Can you help me if you have any answer on how to do it ?
thanks ! (for some reasons I can't say hello on my post ?)
You can use HorizontalPager or VerticalPager from Accompanist.
https://google.github.io/accompanist/pager/
HorizontalPager(
modifier = Modifier.fillMaxSize(),
count = 10,
state = state
) { page ->
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(text = "Screen $page")
}
}
An example https://github.com/google/horologist/blob/e2741cef87774b18d58bb1b0f78bd5b60901f20d/sample/src/main/java/com/google/android/horologist/scratch/ScratchActivity.kt#L79
If you want something more custom, then it's probably a serious investment in a component. Especially if it needs to work with the system Swipe to Dismiss.
I am working on a Food App using Jetpack compose.
When I run the app on Android 12 Pixel 4 Emulator,I noticed that the UI becomes stretched as I scroll up or down.
All was fine until I added code for AnimatedVisibility to expand or collapse a section of screen using this code snippet
#Composable
fun ExpandableMeal(
meal: Meal,
onToggleClick: () -> Unit,
content: #Composable () -> Unit,
modifier: Modifier
) {
Column( modifier = modifier)
{.....
AnimatedVisibility(visible = meal.isExpanded) {
content()
}
...}
I then call this composable inside a LazyColumn to enable scrolling.
LazyColumn(
modifier = Modifier
.fillMaxSize())
{
items(state.meals) { meal ->
ExpandableMeal(
meal = meal,
content = {}
modifier = Modifier.fillMaxWidth()
)
I launched the same code on my physical phone running Android 12 but there was no stretching on the UI Components. I installed Android 13 (Tiramisu) Emulator and there too stretching didn't happen.
Therefore the issue is only on Android 12 Emulator as illustrated on this GIF - (1st few images are for Android 13 Emulator, the rest are snapshots from Android 12 Emulator.)
I will appreciate assistance on how to solve this issue since Android 12 is my main test device, here is the link to the app
I have already viewed other posts on the site. They suggest using the focusRequestor modifier and I've tried that.
val scope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
Text(
modifier = Modifier
.focusable()
.focusTarget() //Tried Adding/Removing
.onFocusChanged {
if (it.hasFocus || it.isFocused) {
Log.i("Test", "Text Focused")
}
} // Tried Adding Visual Elements to delete it as well.
.focusRequester(focusRequester),
text = "MARSK"
)
LaunchedEffect(scope) {
focusRequester.requestFocus()
}
I'm taking Text in this example, but actually I need to implement this on Canvas. This approach is not working on Box too. I'm afraid the same is the case for other containers as well.
For background, this is being built for my TV, so I wish to make this element clickable by the OK button on the remote's D-Pad. My approach is to make it focused first, then assuming that once it is focused, it will automatically detect the press of the OK button as the 'click'. If there is something wrong with my approach, feedback for improvements is also welcome.
PS: The solution by Abhimanyu works like a charm on Mobile Devices. However, since I mentioned a TV above, a TV is the device in consideration. On the TV, I have to press a button (Any button on the DPAD works, even the OK button, strangely) to get it focused up. Any idea how to get round this issue?
Thanks
Issue
The onFocusChanged should be added BEFORE the focusTarget or focusable that is being observed.
Source
Changes
Remove focusTarget and move onFocusChanged before focusable.
Also, note that focusRequester must be before focusable.
This would hopefully work for all comopsables. I have tested using Text and the example from Docs was using Box.
Extra details.
Prefer focusable over focusTarget.
From focusTarget docs,
Note: This is a low level modifier. Before using this consider using Modifier.focusable(). It uses a focusTarget in its implementation. Modifier.focusable() adds semantics that are needed for accessibility.
Order of Modifiers matter
Layouts in Jetpack Compose Codelab
Check Order Matters section
order matters when chaining modifiers as they're applied to the composable they modify from earlier to later,
Sample code
#Composable
fun FocusableText() {
val scope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
var color by remember { mutableStateOf(White) }
LaunchedEffect(scope) {
focusRequester.requestFocus()
}
Text(
modifier = Modifier
.background(color)
.onFocusChanged {
color = if (it.hasFocus || it.isFocused) {
LightGray
} else {
White
}
}
.focusRequester(focusRequester)
.focusable(),
text = "Test Text"
)
}
The Text background is LightGray which indicates the text is focused.
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.