I'm using the Shimmer layout now in my new apps, and its really awesome.
I've started to use this library to handle the shimmer effect with RecyclerView.
But, I dont know how to do use this shimmer effect in a static layout, like a detail activity of a product for example.
I need to set all my TextView's background to gray and programmatically set to normal after a success request?
I really don't get it. Can you help me with this concept?
Ok I will just show you the code and explain it afterwards:
Layout file (only important part):
Kotlin using
kotlinxfor view binding:Java version of the Code (without
findViewById()part):Okay first of all: You do not need the
ShimmerRecyclerViewLibrary for this use case. I have only used theShimmerLayout(build.gradle):My basic idea was to overlap the
Mock Layoutwith theContent Layout. To my knowledge best practice is using aRelativeLayoutrather than aFrameLayoutfor this. To overlap I have setandroid:layout_centerHorizontal="@id/llRealContent"in theMock Layout(I'd advise you to setlayout_centerVerticalas well). TheShimmerFrameLayoutshould represent theRoot-ViewGroupof thisMock Layout.In your Mock Layout you just create your Mock Views (meaning the "grey stripes" signaling the user where to expect content). I have made a very simplistic example but of course it will work on more complex layouts. The mock layout should look as close as possible to the real one (I would advise to just copy & paste the real layout and change every
TextViewetc. to aViewand set the background and size as needed).It is important to set the
visibilityof theRoot-ViewGroupof theContent Layouttogoneso you won't see it and it won't take out any layout space.In the Kotlin/Java code I - for the sake of simplicity - just set the
TextViewtexts with a 10 second delay. As a first step you must activate the shimmer effect viasflMockContent.startShimmerAnimation().Then fetch your content (from a REST-API, database, whatever). Once you have sucessfully fetched AND set all your data in your
Content Layoutyou just have to set the visibility of the Mock Layout to Gone and the visibility of the Content Layout to VisibleVoilah!
(Note that the quality of this code is not great - for the sake of simplicity :D)