https://github.com/karim-eg/glide-slider/tree/master/app
Add this line to root
build.gradle
at allprojects block code:
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
then add this line into your
build.gradle
app level.
dependencies {
implementation "com.github.karim-eg:glide-slider:1.0"
}
Add this widget into your xml
activity file
<co.encept.slider.SliderLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp" />
Add these permessions into your AndroidManifest.xml
file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
Note: If you want to load images from the internet, you need both the INTERNET
and READ_EXTERNAL_STORAGE
permissions to allow files from the internet to be cached into local storage, but if you want to load images from drawable, then no additional permissions are necessary.
Now Open Your activity and add SliderLayout
at class level
private lateinit var mDemoSlider: SliderLayout
at onCreate
, assign the sliderLayout that you just defined before
just type the variable name and assign the slider from xml file by viewBinding or findViewById
mDemoSlider = binding.slider
at onCreate
, Now let's add some item's to the slider.
Define two ArrayLists the first one called itemName
, the secondone called itemUrl
.
val itemName = ArrayList<String>()
val itemUrl = ArrayList<String>()
// 1st item in the slider
itemUrl.add("https://www.revive-adserver.com/media/GitHub.jpg")
itemName.add("JPG Format")
// 2nd item in the slider
itemUrl.add("https://e7.pngegg.com/pngimages/519/64/png-clipart-black-laptop-computer-illustration-computer-programming-web-development-computer-software-programming-language-theme-coder-electronics-gadget.png")
itemName.add("PNG Format")
// 3rd item in the slider
itemUrl.add("https://i.pinimg.com/originals/e4/26/70/e426702edf874b181aced1e2fa5c6cde.gif")
itemName.add("GIF Format")
at onCreate
, Add This Lines below the other code that you've added before
val requestOptions = RequestOptions()
requestOptions.centerInside()
for (pos in itemUrl.indices) {
// if you want show image only without description text use DefaultSliderView(this) instead
val sliderView = TextSliderView(this)
// initialize SliderLayout
sliderView
.image(itemUrl[pos])
.description(itemName[pos])
.setRequestOption(requestOptions)
.setProgressBarVisible(true)
// handle slider click listener:
.setOnSliderClickListener { slider ->
// TODO: Add your click listener implementation here.
runOnUiThread {
Toast.makeText(this, "name: ${slider.bundle.getString("name")}\nurl: ${slider.bundle.getString("url")}",
Toast.LENGTH_SHORT).show()
}
}
// add image url and image name to bundle
sliderView.bundle(Bundle())
sliderView.bundle.putString("name", itemName[pos])
sliderView.bundle.putString("url", itemUrl[pos])
mDemoSlider.addSlider(sliderView)
}
/**
* set slider animation see all Transformer animations at [com.glide.slider.library.SliderLayout.Transformer]
*/
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Tablet)
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom)
mDemoSlider.setCustomAnimation(DescriptionAnimation())
// timer between every slider scroll (in milliseconds)
mDemoSlider.setDuration(4000)
// set true if you want to stop slider cycling when user touch it.
mDemoSlider.stopCyclingWhenTouch(false)
don't forget to close the sliderLayout cycle when activity stops to prevent a memory leak.
override fun onStop() {
super.onStop()
// To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
mDemoSlider.stopAutoCycle()
}
There are some default indicators. If you want to use a provided indicator:
<co.encept.slider.indicators.PagerIndicator
android:id="@+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
now you've implemented slider library into your app if you want more advanced options you can take a look here: ActivityWithImplementations
Powered by Encept Ltd.
* This Library Was Forked From: https://github.com/firdausmaulan/GlideSlider
List of apps on Play Store where this library used. Contact me if you want your app listed.
Icon | Application |
---|---|
Coding Oasis - Learn Programming |