Open In App

How to Remove ListView Item Divider in Android?

Last Updated : 23 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

ListView in Android is generally used for displaying or listing items in the form of a list. Each item in the list is clickable and the list itself is vertically scrollable. In general, each item is separated by a thin line or a horizontal stroke. This line is termed an item divider in Android as it marks the division or separation of two items.

ListView in Android

In this article, we will show you how you could remove this item divider by typing a few lines of code. Follow the process explained below 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. We shall be declaring a ListView as coded below.

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="match_parent"
        android:layout_height="match_parent"/>
  
</RelativeLayout>


Step 3: Working with the list_item.xml file

The ListView above accepts an item in the format of a layout. So, the item is to be developed as a layout and then a TextView is added to display the sample text.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <TextView
        android:id="@+id/item_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>
  
</RelativeLayout>


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. In the main code, to remove the item divider, we need to call the ListView divider and set it to null as shown below. 

Kotlin




package org.geeksforgeeks.myapplication
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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)
          
        // Creating a sample text array
        val mList = arrayOf("One", "Two", "Three", "Four", "Five")
          
        // Creating ListView adapter 
        // and supplying the above array
        mListView.adapter = ArrayAdapter(this, R.layout.list_item, R.id.item_text_view, mList)
  
        // Setting the ListView divider to null
        mListView.divider = null
    }
}


Output:

You can see that the ListView divider is now removed.

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads