Open In App

How to Add Footer to ListView in Android with Kotlin?

Improve
Improve
Like Article
Like
Save
Share
Report

In Android, a ListView is a view used to display a list of items separated by a stroke. A ListView adapter is used to supply items from the main code to the ListView in real-time. A Footer is anything that gets added at the bottom of any element. So, a Footer in ListView is a layout that is present at the bottom of the ListView.

ListView in Android

So in this article, we will show you how you could create a layout and add it as a footer in the ListView in Android. Follow the below steps once the IDE is ready.

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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a ListView to display items.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <ListView
        android:id="@+id/list_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  
</RelativeLayout>


Step 3: Create a layout for Footer (footer.xml)

We added a TextView in this layout.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Footer"
        android:textSize="30sp"
        tools:ignore="MissingConstraints" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the MainActivity.kt file

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

Kotlin




package org.geeksforgeeks.listviewlfooter
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing 
        // the ListView from the layout file
        val mListView = findViewById<ListView>(R.id.list_view_1)
  
        // Declaring a list of items
        val mItems = arrayOf(
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")
  
        // Creating an adapter for ListView
        val mAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, mItems)
          
        // Inflating the Footer file
        val mFooter = layoutInflater.inflate(R.layout.footer, mListView, false) as ViewGroup
  
        // Adding the footer to the ListView
        mListView.addFooterView(mFooter)
          
        // Setting the adapter
        mListView.adapter = mAdapter
    }
}


Output:

You can see that the footer is added at the bottom of the ListView.

Output



Last Updated : 06 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads