Skip to content Skip to sidebar Skip to footer

How To Change Textview Text Color Inside A Listview When List Item Is Clicked?

I am trying to change text color of a TextView inside a listview item. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override pub

Solution 1:

try this code:

public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

publicvoidonItemClick(AdapterView<?> a, View v,
                    int position, long id) {

if (row != null) {
    row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};

Solution 2:

Create a new StateListDrawable like you did before but with black as default color and white when pressed.

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_selected="true"android:color="@color/black" /><itemandroid:state_focused="true"android:color="@color/black" /><itemandroid:state_pressed="true"android:color="@color/black" /><itemandroid:color="@color/white" /></selector>

Now for in the TextView change the text color to the new drawable:

android:textColor="@color/list_item_text"

More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Solution 3:

you will have to set textview as tag to the clickable button as:

...
view.setOnClickListener(this);
view.setTag(textView);
...


publicvoidonclick(View v){
  ...
  TextViewtextView= (TextView)v.getTag();
  textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
  ...
}

Post a Comment for "How To Change Textview Text Color Inside A Listview When List Item Is Clicked?"