PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How to use image placeholders for preview with Jetpack Compose

 May 06, 2022     android, android-jetpack-compose, image     No comments   

Issue

With XML, we had the option to use tools: for designing by using placeholders when real data is not available. Do we have something similar in Jetpack Compose?

I know I can pass sample data to my composable in a dedicated preview function. But for instance, when an image source is a URL (that loads by Coil, Glide.. ), even if I pass a sample URL, it can't be loaded in preview. A practical solution for that could save development time.


Solution

Just as an update on cd1 Answer:

rememberCoilPainter is renamed to rememberImagePainter and its arguments changed

More info about the changes:

  • rememberCoilPainter is renamed to rememberImagePainter and its arguments changed:
  • shouldRefetchOnSizeChange is replaced with onExecute, which has more control over if image requests are executed or skipped.
  • requestBuilder is renamed to builder.
  • fadeIn and fadeInDurationMs are removed. Migrate to ImageRequest.Builder.crossfade.
  • previewPlaceholder is removed. ImageRequest.placeholder is now automatically used if inspection mode is enabled.
  • LoadPainter is renamed to ImagePainter.
  • ImagePainter no longer falls back to executing an image request with the root view's size if onDraw is not called. This is most likely to be noticeable if you use ImagePainter in a LazyColumn and the Image's size isn't constrained. Loader and rememberLoadPainter are removed.
  • LocalImageLoader.current is not-null and returns the singleton ImageLoader by default.
  • DrawablePainter and rememberDrawablePainter are now private.

source: https://coil-kt.github.io/coil/compose/

Regarding placeholder visible in preview, the code is:

Image(
    painter = rememberImagePainter(
        data = "https://www.example.com/image.jpg",
        builder = {
            placeholder(R.drawable.placeholder)
        }
    ),
    contentDescription = "some description",
)


Answered By - Kuruchy
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing