that's my selector for list:(item_selector.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/orange" />
<item android:state_focused="true" android:drawable="@color/orange" />
<item android:state_pressed="true" android:drawable="@color/orange" />
<item android:drawable="@color/transparent" />
</selector>
that's my row of list(row_item.xml)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/alarm_item_drawer_selector"
>
<ImageView
android:id="@+id/item_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:paddingTop="5dp"
android:paddingBottom="2dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/item_title"
style="@style/DrawerItemTextStyle"
android:textColor="@color/text_drawer_item_selector"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_icon" />
</RelativeLayout>
I want to change android:state_pressed="true" android:drawable="@color/orange" to other color dynamically. My goal, to make pressed color for each row different. Is it possible to do?
What I did in a project of mine was I created a static method that takes any view, and changes its background to one of six different colour selectors I made. The trick here is not trying to change the listview selector, you want to change the background of the parent layout of your listview's layout.
Here's an example:
listview.xml (The actual content of the listview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:id="@+id/text"
android:text="Text"
android:padding="20dp"/>
</LinearLayout>
getView() method in my adapter:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder holder = new Holder();
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.listview, viewGroup, false);
} else {
holder = (Holder) view.getTag();
}
holder.parent = (LinearLayout) view.findViewById(R.id.parent);
setRandomListViewSelector(holder.parent);
holder.text = (TextView) view.findViewById(R.id.text);
holder.text.setText(strs.get(i));
return view;
}
Static method that changes the background:
public static void setRandomListViewSelector(View parentView) {
int i = new Random().nextInt(2);
switch (i) {
case 0:
parentView.setBackgroundResource(R.drawable.list_selector_blue);
break;
case 1:
parentView.setBackgroundResource(R.drawable.list_selector_red);
break;
}
}
list_selector_blue.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_blue_dark"/>
</shape>
</item>
</selector>
In my example, I just use the Random class to randomly generate a number between 0 and 1. If you'd like more control, you could create a global variable and increment that by 1 so that you have better control over what background colours are set.
Affect to each item a new StateListDrawable.
StateListDrawable stateListDrawable= new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {android.R.attr.state_selected}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
stateListDrawable.addState(new int[] {}, new ColorDrawable(getContext().getResources().getColor(R.color.anycolor)));
view.setBackgroundDrawable(stateListDrawable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With