Just configure a singleton loading instance once without additional boilerplate code and reuse it to show and hide from anywhere (Both Activity or Fragment) you want .
Step 1. Add the JitPack repository to your root build.gradle
at the end of repositories
android {
.
.
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
.
.
Step 2. Add the dependency
dependencies {
implementation 'com.github.Gkemon:Easy-Android-Loading:1.1'
}
class MainActivity : AppCompatActivity() {
.
.
.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/* It is the mandatory configuration which is needed to be
* declared after calling setContentView --*/
LoadingPopup.getInstance(activity)
.defaultLovelyLoading()
.build()
/*OR*/
/* If adding background color or opacity is needed then -- */
LoadingPopup.getInstance(activity)
.defaultLovelyLoading()
.setBackgroundColor(android.R.color.holo_red_dark)
.setBackgroundOpacity(myBackgroundOpacity)/*Int between 0-100*/
.build()
.
.
}
class MainActivity : AppCompatActivity() {
.
.
.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
LoadingPopup.getInstance(activity)
.customLoading()
.setCustomViewID(R.layout.layout_my_custom_loading)
/*layout resource id which holds the custom loading view. If setting up
*background color is needed for the inputted layout then call it like that
*setCustomViewID(R.layout.layout_my_custom_loading,R.color.my_color)*/
.doIntentionalDelay()
/*If intentional delay is needed. Otherwise call .noIntentionalDelay()*/
.setDelayDurationInMillSec(5000)
.setBackgroundOpacity(myBackgroundOpacity)
.build()
.
.
}
/* For showing loading just call --> */
LoadingPopup.showLoadingPopUp()
/* For hiding loading just call --> */
LoadingPopup.hideLoadingPopUp()
Logo credit: Alex Gorbunov
The source code is licensed under the Apache License 2.0.