SkeletonLayout Documentation

repository·develop·Indexed 20 days ago

https://github.com/faltenreich/skeletonlayout

An Android library that implements the Skeleton View pattern to provide visual feedback during asynchronous data loading. It supports shimmer and pulse animations, custom mask layouts, and provides specialized integration for RecyclerView and ViewPager2 via the SkeletonConfig class and SkeletonLayoutUtils.

Tokens
2.8K
Snippets
6
Records
8
Agent score
20%

What's inside SkeletonLayout

  1. Install SkeletonLayout via Gradle

    develop

    To use SkeletonLayout in your Android project, add mavenCentral() to your buildscript repositories and include the dependency in your app's build.gradle file.

    Note: The library requires org.jetbrains.kotlin:kotlin-stdlib-jdk7, androidx.recyclerview:recyclerview, and androidx.viewpager2.widget.ViewPager2 as dependencies.

    buildscript {
        repositories {
            mavenCentral()
        }
    }
    
    dependencies {
        implementation 'com.faltenreich:skeletonlayout:<version>'
    }
  2. Use SkeletonLayout in Java

    develop

    In Java, use SkeletonLayoutUtils to create or apply skeletons. Use createSkeleton() for general views and applySkeleton() for RecyclerView or ViewPager2. Use showSkeleton() to start the loading state and showOriginal() to hide it.

    public class MainActivity extends AppCompatActivity {
        
        private Skeleton skeleton;
        
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            // Either use an existing Skeletonlayout
            skeleton = findViewById(R.id.skeletonLayout);
            
            // or create a new SkeletonLayout from a given View
            skeleton = SkeletonLayoutUtils.createSkeleton(view);
    
            // and customize every detail including the layout used for the skeleton mask
            skeleton = SkeletonLayoutUtils.createSkeleton(view, SkeletonConfig.Companion.defaults(this, R.layout.custom_mask));
    
            // support ViewPager2
            skeleton = SkeletonLayoutUtils.applySkeleton(viewPager2, R.layout.list_item_viewpager2);
    
            // support RecyclerView
            skeleton = SkeletonLayoutUtils.applySkeleton(recyclerView, R.layout.list_item_recyclerview);
    
            skeleton.showSkeleton();
        }
        
        // Example callback that hides skeleton
        private void onDataLoaded() {
            skeleton.showOriginal();
        }
    }
  3. Implement SkeletonLayout in XML

    develop

    You can wrap views that you want to mask inside a com.faltenreich.skeletonlayout.SkeletonLayout in your layout XML files. The SkeletonLayout will act as a container for the views to be masked.

    <com.faltenreich.skeletonlayout.SkeletonLayout
        android:id@"@+id/skeletonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <!-- Views to mask -->
        
    </com.faltenreich.skeletonlayout.SkeletonLayout>
    <com.faltenreich.skeletonlayout.SkeletonLayout
        android:id="@+id/skeletonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <!-- Views to mask -->
        
    </com.faltenreich.skeletonlayout.SkeletonLayout>
  4. Use SkeletonLayout in Kotlin

    develop

    You can use SkeletonLayout in Kotlin by either finding an existing SkeletonLayout in your view hierarchy or creating a new one from an existing View using the createSkeleton() extension function. For RecyclerView and ViewPager2, use the applySkeleton() extension function.

    To toggle the view, use showSkeleton() to display the loading state and showOriginal() to return to the actual content once data is loaded.

    class MainActivity : AppCompatActivity() {
    
        private lateinit var skeleton: Skeleton
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // Either use an existing Skeletonlayout
            skeleton = findViewById(R.id.skeletonLayout)
    
            // or create a new SkeletonLayout from a given View
            skeleton = view.createSkeleton()
    
            // and customize every detail including the layout used for the skeleton mask
            skeleton = view.createSkeleton(config = SkeletonConfig.default(this, maskLayout = R.layout.custom_mask))
            
            // support ViewPager2
            skeleton = viewPager2.applySkeleton(R.layout.list_item_viewpager2)
    
            // support RecyclerView
            skeleton = recyclerView.applySkeleton(R.layout.list_item_recyclerview)
            
            skeleton.showSkeleton()
        }
    
        // Example callback that hides skeleton
        private fun onDataLoaded() {
            skeleton.showOriginal()
        }
    }
  5. Configure SkeletonLayout properties

    develop

    You can customize the appearance and behavior of the skeleton using the following properties:

    PropertyTypeDescription
    maskLayoutreferenceOptional reference to layout resource that should be masked when skeleton is applied (defaults to wrapped view if null)
    maskColorcolorColor of the mask that fills the original view bounds (defaults to #E0E0E0)
    maskCornerRadiusdimensionThe x- and y-radius of the oval used to round the mask corners (defaults to 25)
    showShimmerbooleanAnimate shimmer if set to true (defaults to true)
    shimmerColorcolorColor of the animated shimmer (defaults to #d5d5d5)
    shimmerDurationInMillisintegerDuration in milliseconds for one shimmer animation interval (defaults to 2000)
    shimmerDirectionenumDirection of animated shimmer (defaults to LEFT_TO_RIGHT)
    shimmerAngleintegerAngle in degrees for animated shimmer (defaults to 0)
    itemCountintegerItem count for Skeleton adapter (RecyclerView and ViewPager2 only, defaults to 3)
  6. Create a default SkeletonConfig

    develop

    Use the SkeletonConfig.default(context, maskLayout) companion method to generate a configuration object pre-populated with the library's default values. This is the recommended way to initialize a configuration unless you need to override every single parameter manually.

    • context: An Android Context used to resolve default color resources.
    • maskLayout: (Optional) An @LayoutRes integer specifying a custom layout for the mask.
    // Using default values from SkeletonLayout constants
    val config = SkeletonConfig.default(context)
    
    // Using default values but providing a specific mask layout
    val customMaskConfig = SkeletonConfig.default(context, R.layout.my_custom_mask)
  7. Configure Skeleton appearance with SkeletonConfig

    develop

    The SkeletonConfig class allows you to define the visual style and animation behavior of the skeleton layout. It implements the SkeletonStyle interface and supports both shimmer and pulse animations. You can create a configuration using the SkeletonConfig.default(context) factory method to start with standard library defaults, or instantiate it directly to customize all parameters.

    Because properties in SkeletonConfig use observable delegates, you can register a callback using addValueObserver(onValueChanged: () -> Unit) to react whenever any configuration property is updated.

    // Create a default configuration
    val config = SkeletonConfig.default(context)
    
    // Register an observer to react to changes
    config.addValueObserver {
        // Update UI or re-apply skeleton if needed
    }
    
    // Customize properties
    config.maskColor = Color.RED
    config.showShimmer = true
    config.pulseMinAlpha = 0.3f
  8. Reference SkeletonConfig properties

    develop

    The following properties are available in SkeletonConfig to control the skeleton's appearance and animation behavior:

    PropertyTypeDescription
    maskLayout@LayoutRes Int?Layout resource used for the mask (defaults to the original view if null)
    maskColor@ColorInt IntThe color of the skeleton mask
    maskCornerRadiusFloatCorner radius for the mask
    showShimmerBooleanWhether to enable the shimmer animation
    shimmerColor@ColorInt IntThe color of the shimmer effect
    shimmerDurationInMillisLongDuration of one shimmer cycle in milliseconds
    shimmerDirectionSkeletonShimmerDirectionThe direction of the shimmer movement
    shimmerAngleIntThe angle of the shimmer effect
    showPulseBooleanWhether to enable the pulse animation
    pulseDurationInMillisLongDuration of one pulse cycle in milliseconds
    pulseMinAlphaFloatThe minimum alpha value during a pulse
    pulseMaxAlphaFloatThe maximum alpha value during a pulse