Change listview Item text color android programmatically

How to create and modify ListView with different item value text color using #color code.

In this tutorial we are modifying all the items text color in list view with the use of setting up current view as textview and after that change the listview text color. So here is the complete step by step tutorial for Change listview Item text color android programmatically.

android-project-download-code-button

How to Change listview Item text color android programmatically.

Code for MainActivity.java file.

 package com.changelistviewitemtextcolor_android_examples.com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.ViewGroup;

public class MainActivity extends Activity {
 
 ListView listView;
 String[] listValue = new String[] 
 {
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX"
 };
 
 List<String> LISTSTRING;
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listView = (ListView)findViewById(R.id.listView1);
 
 LISTSTRING = new ArrayList<String>(Arrays.asList(listValue));
 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, LISTSTRING){
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent){
 
 View view = super.getView(position, convertView, parent);

 TextView ListItemShow = (TextView) view.findViewById(android.R.id.text1);

 ListItemShow.setTextColor(Color.parseColor("#fe00fb"));

 return view;
 }
 
 };
 
 listView.setAdapter(adapter); 
 
 
 
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.changelistviewitemtextcolor_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

</RelativeLayout>

Screenshot:

Change listview Item text color android programmatically

Click here to download Change listview Item text color android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *