FirebaseUI-Android
firebase • Updated Nov 10, 2025
Add Dependency
// FirebaseUI for Firebase Realtime Database implementation ("com.firebaseui:firebase-ui-database:8.0.2")
Retrieve Data from FireBase DataBase
val path = "Creative/katargam" val query: Query = FirebaseDatabase.getInstance().reference.child(path)
Using FirebaseUI to populate a RecyclerView
If you're displaying a list of data, you likely want to bind the
MyData objects to a RecyclerView.
This means implementing a custom RecyclerView.Adapter and coordinating updates with the
ChildEventListener.Fear not, FirebaseUI does all of this for you automatically!
Choosing an adapter
FirebaseUI offers two types of RecyclerView adapters for the Realtime Database:
FirebaseRecyclerAdapter— binds aQueryto aRecyclerViewand responds to all real-time events included items being added, removed, moved, or changed. Best used with small result sets since all results are loaded at once.
FirebaseRecyclerPagingAdapter— binds aQueryto aRecyclerViewby loading data in pages. Best used with large, static data sets. Real-time events are not respected by this adapter, so it will not detect new/removed items or changes to items already loaded.
Using the FirebaseRecyclerAdapter
The
FirebaseRecyclerAdapter binds a Query to a RecyclerView. When data is added, removed, or changed these updates are automatically applied to your UI in real time.First, configure the adapter by building
FirebaseRecyclerOptions.// class level scope lateinit var options: FirebaseRecyclerOptions<Mydata?> // in onCreate intialize this options = FirebaseRecyclerOptions.Builder<Mydata>() .setQuery(query, Mydata::class.java).build()
Next create the
FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass
for displaying each item.// in some case you need to specify type explicitly than use Any data type for internal type // val adapter: FirebaseRecyclerAdapter<*, *> = ... // othervise this will work in 99% situations. val adapter = object : FirebaseRecyclerAdapter<Mydata?, MyViewHolder>(options) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { // Create a new instance of the ViewHolder, in this case we are using a custom // layout called R.layout.message for each item val view: View = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return MyViewHolder(view) } override fun onBindViewHolder(holder: MyViewHolder, position: Int, model: Mydata) { holder.name.text = model.name Glide.with(this@ViewActivity).load(model.imageurl).into(holder.imgeddd) } } class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var imgeddd: ImageView var name: TextView init { imgeddd = itemView.findViewById(R.id.imgeddd) name = itemView.findViewById(R.id.name) } } }
FirebaseRecyclerAdapter lifecycle
Start/stop listening
The
FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query.
To begin listening for data, call the startListening() method. You may want to call this in your
onStart() method. Make sure you have finished any authentication necessary to read the data
before calling startListening() or your query will fail.Similarly, the
stopListening() call removes the event listener and all data in the adapter.
Call this method when the containing Activity or Fragment stops:override fun onStart() { super.onStart() adapter.startListening(); } override fun onStop() { super.onStop() adapter.stopListening(); }
Change query and fetch data using firebase recyclerview
updateOptions()method of adapter allows us to change query of what data to fetch from database by some conditions , this new query will be passed to newoptionsand after thatupdateOptions( new_option )method called to fetch new data ,
adapter.notifyDataSetChanged()method will make new view Item with new data and change recyclerView in UI that looks changes are made.
private fun changeFirList() { // in this example we have sorted data between on start date and end date // from based on registerDate colummn val sDate = binding.startDate.text.toString() val eDate = binding.endDate.text.toString() query = FirebaseDatabase.getInstance() .getReference() .child("FIR/Data/${cUser!!.uid}") .orderByChild("registerDate") .startAt(sDate) .endAt(eDate) options = FirebaseRecyclerOptions.Builder<FIR>() .setQuery(query, FIR::class.java) .build() adapter.updateOptions(options) adapter.notifyDataSetChanged() }
