Monday, August 10, 2015

How to change ListView item text color in Android

Change ListView Item Text Color
ListView shows a list of items in an Android application user interface. ListView shows multiple items at a time. This is a vertically scrollable widget. Items are positioned sequentially. One immediately below another item. By default, the ListView widget is looking good and standard. But sometimes, android app developers want to change the default font/text color of ListView element items.

This android app development tutorial will demonstrate and describe to you how can we change the ListView item’s text color. But there has no direct way or a single attribute or a single method to change the ListView item text color. So we have to write a few lines of code to get the result.

In the first step, we add a ListView widget to our XML layout file. Next, in the java file, we do our sequence of jobs to apply a specific color to ListView items. ListView items are populated from an array adapter. And an array adapter builds using an array list and an array list gets elements from an array.

The actual code we write while we initialize the ArrayAdapter. Here we override the ArrayAdapter getView method and cast the view instance as a TextView widget. Now, we set the text color for the TextView widget. This is the color that will show in our ListView widget’s item text color. This TextView act as a single item of the ListView element. So, finally, we will get our desired ListView with the specified text color.
MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        ListView listView = findViewById(R.id.listView);


        // Initializing a new String Array
        String[] fruits = new String[] {
                "African mango",
                "Ambarella",
                "American Black Elderberry",
                "Ackee",
                "American persimmon",
                "Babaco"
        };


        // Create a List from String Array elements
        List<String> fruits_list = new ArrayList<String>
                (Arrays.asList(fruits));


        // Create an ArrayAdapter from List
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (
                        this,
                        android.R.layout.simple_list_item_1,
                        fruits_list
                ){
            @Override
            public View getView(
                    int position, View convertView, ViewGroup parent){
                // Get the Item from ListView
                View view = super.getView(position, convertView, parent);

                // Initialize a TextView for ListView each Item
                TextView textView = (TextView) view
                        .findViewById(android.R.id.text1);

                // Set the text color of TextView (ListView Item)
                textView.setTextColor(Color.parseColor("#5218FA"));

                // Generate ListView Item using TextView
                return view;
            }
        };

        // DataBind ListView with items from ArrayAdapter
        listView.setAdapter(arrayAdapter);
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>