I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.<
after many attempts set color in navigation view, I found out the simplest way to set text color in listview, maybe it will help to someone
res/color/listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is used when the navigation item is selected -->
<item android:state_enabled="true"
android:state_activated="true" android:color="@color/colorAccent" />
<!-- This is the default text color -->
<item
android:color="@android:color/black" />
</selector>
and set in TextView as android:textColor:
<TextView
android:id="@+id/tvItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_vertical_extra_margin"
android:textColor="@color/listview_selector"
android:textSize="@dimen/text_size_normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Row item text" />
the trouble was in the right state, use android:state_activated in navigation view
Create following button_text.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Change your text view's textColor to:
<TextView
android:id="@+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/rihanna_love_the_way_lie"
android:textColor="@color/button_text" //point to that xml
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
You can read about color state list here
Implement setOnItemClickListener
for the listView
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long arg3) {
RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
TextView textView = (TextView) relativeLayout.getChildAt(0);
ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);
textView.setTextColor(Color.RED);
// Perform whatever action for your textView and imageView
}
});
Like others have said, just change the color in your onItemClick method. Additionally:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
tv.setTextColor(Color.BLUE);
}
public void setItemNormal()
{
for (int i=0; i< mDrawerList.getChildCount(); i++)
{
View v = mDrawerList.getChildAt(i);
//TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
txtview.setTextColor(Color.BLACK);
}
}
by doing this we set all items to normal before we change it on each item press.
For that you've to set OnClickListener for that particular ImageView and then change TextView's color on click of ImageView in Adapter Class.
Alternatively,
mList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TextView tv= view.findViewById(resID);
tv.setTextColor(color)
}
});