We have seen many apps that display images in the slider format as that of banners which slide automatically. This type of feature of the auto image slider is seen in many E-commerce sites. This feature is seen in many apps which are having images in them. In this article, we will take a look at How to Create a dynamic Auto Image Slider in Android. So we will be using Firebase to load the images inside our slider with Firebase. With the help of Firebase, these images can be updated in real-time without updating the app. For the implementation of SliderView, we will be using the SliderView library.
What we are going to build in this article?
We will be creating a simple application in which we will be creating a slider view and we will be displaying images from Firebase inside our SliderView. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a new Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.
Inside that column Navigate to Firebase Cloud Firestore. Click on that option and you will get to see two options on Connect app to Firebase and Add Cloud Firestore to your app. Click on Connect now option and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase. After connecting your app to Firebase you will get to see the below screen.
After that verify that dependency for Firebase Firestore database has been added to our Gradle file. Navigate to the app > Gradle Scripts inside that file to check whether the below dependency is added or not. If the below dependency is not present in your build.gradle file. Add the below dependency in the dependencies section.
After adding this dependency we will add the dependency for our SliderView and Picasso which will be used to load images from their image URL.
After adding these dependencies sync your project. Now we will move towards the AndroidManifest.xml part of the app.
Step 3: Working with AndroidManifest.xml file
For adding data to Firebase we should have to give permissions for accessing the internet. For adding these permissions navigate to the app > AndroidManifest.xml and Inside that file add the below permissions to it.
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 5: Creating a Modal Class for storing the URL of our images in Slider
Now we will create a modal class for storing our URLs for our images. For creating a new java class. Navigate to the app > java > your app’s package name and Right-click on it and click on New > Java Class. Give a name to your java class and add the below code to it. Here we have given the name as SliderData. Below is the code for the SliderData.java file.
Step 6: Create a layout file for our items inside our SliderView
For creating a new layout file for displaying inside our SliderView, navigate to the app > res > layout > Right-click on it and click on New > Layout Resource file and give a name to your file. Here we have given the file name as image_slider_item and add the below code to it.
Step 7: Creating an Adapter class for setting data inside our Slider items
For creating an Adapter class, navigate to the app > java > your app’s package name > Right-click on it > New > java class and name your class as SliderAdapter and add below code to it.
Step 8: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 9: Adding the data to our Firebase Firestore Console
Go to Firebase and click on Go to Console option on the top right corner.
After clicking on this screen you will get to see the below screen with your all project inside that select your project.
Inside that screen click n Firebase Firestore Database in the left window.
After clicking on the Create Database option you will get to see the below screen.
Inside this screen, we have to select the Start in test mode option. We are using test mode because we are not setting authentication inside our app. So we are selecting Start in test mode. After selecting test mode click on the Next option and you will get to see the below screen.
Inside this screen, we just have to click on the Enable button to enable our Firebase Firestore database. After completing this process we just have to add data inside our Firebase Firestore console for adding images to our slider. For adding data click on the Start collection option and you will get to see the below screen.
After adding this data click on the Next option and you will get to see the below screen.
Add the data inside our Firebase Firestore console and click on save. Your first image to the slider has been added. Similarly, add more images by clicking on the “Add document” button.
After adding these images run your app and you will get to see the output on the below screen.