Shimmer Loading Effect kotlin

07-Jan-2025

Shimmer Loading Effect kotlin

Output: 



1/  Create A New Project. and Implement this Library and sync the file




implementation("com.facebook.shimmer:shimmer:0.5.0")



2/  full code of build.gradle file



plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.microappvalley.shimmerloadingeffectkotlin"
compileSdk = 35

defaultConfig {
applicationId = "com.microappvalley.shimmerloadingeffectkotlin"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

implementation("com.facebook.shimmer:shimmer:0.5.0")

}



3/  Create and modify the user_item_layout.xml file



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="@drawable/baseline_person_24"/>

<TextView
android:layout_marginTop="10dp"
android:layout_width="190sp"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerInParent="true"
android:text="This is a person"
android:textSize="20sp"
android:background="#dddddd"/>


</RelativeLayout>




4/  Create and Modify the user_item_shimmer_layout.xml file



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
>

<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="#767676" />

<TextView
android:layout_width="190sp"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:background="#dddddd"
android:text=""
android:textSize="20sp" />

</RelativeLayout>




5/ Modify the activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/user_item_shimmer_layout"/>
</com.facebook.shimmer.ShimmerFrameLayout>

<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/user_item_layout"/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>



6/ Modify the MainActivity.kt file




package com.microappvalley.shimmerloadingeffectkotlin

import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.LinearLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.facebook.shimmer.ShimmerFrameLayout


class MainActivity : AppCompatActivity() {
lateinit var shimmer: ShimmerFrameLayout
lateinit var linear: LinearLayout

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

shimmer= findViewById(R.id.shimmer)
linear= findViewById(R.id.linear)

linear.visibility = View.INVISIBLE
shimmer.startShimmer()

Handler().postDelayed({
linear.visibility = View.VISIBLE
shimmer.stopShimmer();
shimmer.visibility = View.INVISIBLE
}, 2000)
}
}



Comments