Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change color of (List item)TextView in Listview

I am using a ListView as shown below in filename browse.xml.

<ListView
   android:id="@+id/listView1"
   android:layout_width="250dp"
   android:layout_height="match_parent"
   android:layout_alignParentLeft="true"
   android:layout_below="@+id/relativeLayout1" >

</ListView>

And I am filling this listView inside onCreate() method as:

files1=new ArrayList<String>();
File sdcard=Environment.getExternalStorageDirectory();
files1 =  getListFiles(new File(sdcard.getAbsolutePath()+File.separatorChar)); 
ArrayAdapter<String> fileList =new ArrayAdapter<String>(this, R.layout.row,files1);

setListAdapter(fileList);

row.xml is shown below as:

<?xml version="1.0" encoding="utf-8"?>
<TextView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/rowtext"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:textSize="20dp" 
   android:textColor="#000000"   
   android:background="#FFFFFF"/>

This whole program is am showing all the files of sdcard at listView and on click of any listitem, am saving that file name into sharedPrefernce..Now I want to change the text color of file name(List items) in the ListView which are there in SharedPrefernce..

[EDIT]: Here am using ArrayAdapter default constructor to list all the items in a listview

Pls suggest me something.. Thanks..

like image 857
Kanika Avatar asked Mar 13 '12 06:03

Kanika


2 Answers

We can change the textcolor dynamically for list item in the getView() of Adapter.

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
    }

    TextView listTitle = (TextView) row.findViewById(R.id.rowtext);
    listTitle.setTextColor(Color.parseColor("#405478"));

    return listTitle;
}
like image 137
RajaReddy PolamReddy Avatar answered Oct 24 '22 05:10

RajaReddy PolamReddy


FIRST

Take one

ArrayList<boolean> saved = new ArrayList<boolean>();

first set All element of saved means 0 to files1.size() to FALSE

SECOND

now when in itemClickListener set TRUE at position clicked in saved like, saved.set(position,TRUE);

and call notifyDataSetChanged(); in that listenher after setting TRUE at that position.

THIRD

now in getView of Adapter Class

public View getView(int position, View convertView, ViewGroup parent) { 

    View row = convertView; 
    position = = getItemViewType(position);
    if(row==null){ 
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null); 
    } 

    TextView listTitle = (TextView) row.findViewById(R.id.rowtext); 
    if(saved.get(position)==TRUE)
    {
            listTitle .setTextColor(Color.parseColor("#405478")); 
    }
}

and also add this in your adapter class,

@Override
public int getItemViewType(int position) {

return position;
}

@Override
public int getViewTypeCount() {
return files1.size();
}
like image 45
MKJParekh Avatar answered Oct 24 '22 04:10

MKJParekh