!!! warning This library is deprecated, and the API is no longer maintained. We recommend forking the implementation and customising it to your needs. The original documentation is below.
A library which provides a modifier for display ‘placeholder’ UI while content is loading.
More information on the UX provided by this library can be found on the Material Theming Placeholder UI guidelines.
There are actually two versions of the library available:
!!! tip You only need to use one of the libraries, and most apps should use Placeholder Material. The APIs of the libraries are (mostly) equivalent with only the imports being different. Where possible we have provided equivalent code samples below.
At the most basic usage, the modifier will draw a shape over your composable content, filled with the provided color.
=== “Placeholder Material”
``` kotlin import com.google.accompanist.placeholder.material.placeholder Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder(visible = true) ) ```
=== “Placeholder Foundation”
``` kotlin import com.google.accompanist.placeholder.placeholder Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder( visible = true, color = Color.Gray, // optional, defaults to RectangleShape shape = RoundedCornerShape(4.dp), ) ) ```
The library also provides some ‘highlight’ animations to entertain the user while they are waiting. There are two provided by the library, but you can also provide your own.
This highlight fades a color over the entire placeholder in and out.
=== “Placeholder Material”
``` kotlin import com.google.accompanist.placeholder.PlaceholderHighlight import com.google.accompanist.placeholder.material.placeholder import com.google.accompanist.placeholder.material.fade Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder( visible = true, highlight = PlaceholderHighlight.fade(), ) ) ```
=== “Placeholder Foundation”
``` kotlin import com.google.accompanist.placeholder.PlaceholderHighlight import com.google.accompanist.placeholder.placeholder import com.google.accompanist.placeholder.fade Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder( visible = true, color = Color.Gray, // optional, defaults to RectangleShape shape = RoundedCornerShape(4.dp), highlight = PlaceholderHighlight.fade( highlightColor = Color.White, ), ) ) ```
This displays a gradient shimmer effect which emanates from the top-start corner.
=== “Placeholder Material”
``` kotlin import com.google.accompanist.placeholder.PlaceholderHighlight import com.google.accompanist.placeholder.material.placeholder import com.google.accompanist.placeholder.material.shimmer Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder( visible = true, highlight = PlaceholderHighlight.shimmer(), ) ) ```
=== “Placeholder Foundation”
``` kotlin import com.google.accompanist.placeholder.PlaceholderHighlight import com.google.accompanist.placeholder.placeholder import com.google.accompanist.placeholder.shimmer Text( text = "Content to display after content has loaded", modifier = Modifier .padding(16.dp) .placeholder( visible = true, color = Color.Gray, // optional, defaults to RectangleShape shape = RoundedCornerShape(4.dp), highlight = PlaceholderHighlight.shimmer( highlightColor = Color.White, ), ) ) ```
repositories { mavenCentral() } dependencies { // If you're using Material, use accompanist-placeholder-material implementation "com.google.accompanist:accompanist-placeholder-material:<version>" // Otherwise use the foundation version implementation "com.google.accompanist:accompanist-placeholder:<version>" }
Snapshots of the current development version of this library are available, which track the latest commit. See here for more information on how to use them.
Please contribute! We will gladly review any pull requests. Make sure to read the Contributing page first though.
Copyright 2021 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.