Skip to content
Related Articles
Open in App
Not now

Related Articles

Android Jetpack Compose – Retrieve Data From Firebase Realtime Database in ListView

Improve Article
Save Article
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article

Firebase Realtime Database provides us with a feature to give Real-time updates to your data inside your app within milliseconds. With the help of Firebase, you can provide Real-time updates to your users. In this article, we will take a look at the implementation of the Firebase Realtime Database for our ListView in Android using Jetpack Compose. In this ListView, we will be able to add and remove the list items on a real-time basis. So let’s move towards the implementation of our Firebase Realtime Database in ListView. A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Adding a new color in the Color.kt file

Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.


package com.example.newcanaryproject.ui.theme
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)

Step 3: 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 Realtime Database. Click on that option and you will get to see two options Connect the app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase.


After completing this process you will get to see the below screen.


Now verify whether your app is connected to Firebase or not. Go to your build.gradle file. Navigate to the app > Gradle Scripts > build.gradle file and make sure that the below dependency is added in your dependencies section.

implementation platform('')
implementation ''

If the above dependency is not added to your dependencies section. Add this dependency and sync your project.

Step 4: Add internet permission in your AndroidManifest.xml file

Add the permission for the internet in the AndroidManifest.xml file. 


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Step 5: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


package com.example.firebaseproject
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.Nullable
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.firebaseproject.ui.theme.FirebaseProjectTheme
import com.example.firebaseproject.ui.theme.greenColor
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            FirebaseProjectTheme {
                // A surface container using the 'background' color from the theme
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
                    // on the below line we are specifying
                    // the theme as the scaffold.
                        // in scaffold we are specifying the top bar.
                        topBar = {
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
                                // along with that we are specifying title for our top bar.
                                title = {
                                    // in the top bar we are specifying tile as a text
                                        // on below line we are specifying
                                        // text to display in top app bar
                                        text = "GFG",
                                        // on below line we are specifying
                                        // modifier to fill max width
                                        modifier = Modifier.fillMaxWidth(),
                                        // on below line we are specifying
                                        // text alignment
                                        textAlign = TextAlign.Center,
                                        // on below line we are specifying
                                        // color for our text.
                                        color = Color.White
                        }) {
                        // on below line creating variable for list of data.
                        var languageList = mutableStateListOf<String?>()
                        // on below line creating variable for freebase database
                        // and database reference.
                        val firebaseDatabase = FirebaseDatabase.getInstance();
                        val databaseReference = firebaseDatabase.getReference();
                        // on below line adding child event listener for our
                        // database reference to read data.
                        databaseReference.addChildEventListener(object : ChildEventListener {
                            override fun onChildAdded(
                                snapshot: DataSnapshot, @Nullable previousChildName: String?
                            ) {
                                // this method is called when new child is added to
                                // our data base and after adding new child
                                // we are adding that item inside our  list
                            override fun onChildChanged(
                                snapshot: DataSnapshot, @Nullable previousChildName: String?
                            ) {
                                // this method is called when the new child is added.
                                // when the new child is added to our list we will be called
                            override fun onChildRemoved(snapshot: DataSnapshot) {
                                // below method is called when we remove a child from our database.
                                // inside this method we are removing the child from our array list
                                // by comparing with it's value.
                                // after removing the data we are notifying our adapter that the
                                // data has been changed.
                            override fun onChildMoved(
                                snapshot: DataSnapshot, @Nullable previousChildName: String?
                            ) {
                                // this method is called when we move our
                                // child in our database.
                                // in our code we are not moving any child.
                            override fun onCancelled(error: DatabaseError) {
                                // this method is called when we get any
                                // error from Firebase with error.
                        // on below line we are calling method to display UI
                        firebaseUI(LocalContext.current, languageList)
fun firebaseUI(context: Context, languageList: SnapshotStateList<String?>) {
    // on below line creating a column
    // to display our retrieved list.
        // adding modifier for our column
        modifier = Modifier
        // on below line adding vertical and horizontal alignment for column.
        verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally
    ) {
            text = "Dynamic ListView Example",
            // in modifier we are specifying padding
            // for our text from all sides.
            modifier = Modifier.padding(10.dp),
            // on below line we are specifying
            // style for our text
            style = TextStyle(
                color = Color.Black, fontSize = 20.sp
            ), fontWeight = FontWeight.Bold
        // on below line we are calling lazy column
        // for displaying listview.
        LazyColumn {
            // on below line we are populating
            // items for listview.
            items(languageList) { language ->
                // on below line we are specifying ui for each item of list view.
                // we are specifying a simple text for each item of our list view.
                Text("" + language, modifier = Modifier.padding(15.dp))
                // on below line we are specifying
                // divider for each list item

After adding this code to your app. Now go to Firebase and click on Go to console option which is in 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 Realtime Database in the left window.  


After clicking on this option you will get to see the screen on the right side. On this page click on the Rules option which is present in the top bar. You will get to see the below screen.


In this project, we are adding our rules as true for reading as well as a write because we are not using any authentication to verify our user. So we are currently setting it to true to test our application. After changing your rules. Click on the publish button at the top right corner and your rules will be saved there. Now again come back to the Data tab. Now we will be adding our data to Firebase manually from Firebase itself.

Step 6: Adding data in Firebase Console

Inside Firebase Database the parent node which we will get to see is a parent. Inside this parent, we have to add child items. This child item will be displayed inside our ListView. We will be adding new child items to our Database. To add the data for our list view. We simply have to add an item by clicking on the “+” option on the right side and after that add the item inside our database. In a similar way add multiple items to your Database. These items will be displayed inside our ListView. 


The above mentioned is the image in which you will get to know How to add a single item in your listview. Similarly add multiple items in your Database and you will get to see the below screen.


After adding this data run your app and see the output of the app. After running your app update the data in your Firebase. You can add, remove any child or update any child, you will get to see Realtime Updates in your ListView.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!