Open In App

How to Change the ListView Text Color in Android?

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

In Android, a ListView is a UI element used to display the list of items. This list is vertically scrollable and each item in the ListView is operable. A ListView adapter is used to supply items from the main code to the ListView in real-time. By default, the TextView font size is 14 sp and color is “@android:color/tab_indicator_text”.

ListView  in Android

In this article, we will show you how you could change the ListView text color 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. We have implemented a ListView in the main layout file.

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"
    tools:context=".MainActivity">
  
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the list_item.xml file

Below is the code for the Item layout for displaying the item in the ListView. We have added textColor and textSize attributes to the TextView to change the text color and size.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="30sp"
        android:textStyle="bold" />
  
</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, we have primarily declared an array and supplied the array items to the ListView with the help of an adapter.

Kotlin




package org.geeksforgeeks.changelistviewtextcolor
  
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)
  
        // Array of desired items
        val mItems: Array<String> = arrayOf("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Zero")
             
          // Declaring and initializing the 
          // ListView from the layout file
          val mListView = findViewById<ListView>(R.id.list_view)
          
          // Creating a ListView adapter
          val mAdapter = ArrayAdapter<String>(this, R.layout.list_item, R.id.text_view, mItems)
          
          // Setting the ListView adapter 
          // with the one created above
          mListView.adapter = mAdapter
    }
}


Output:

You can see that the text color and size have changed in the ListView.

Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads