Unable to set color on android: state_enabled = "false"

I have a ListView, and for the first time I show it. I want to disable some of its elements.

For this, I did this textorange_selected.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:color="@color/medium_gray" /> <item android:state_selected="true" android:color="@android:color/white" /> <item android:state_focused="true" android:color="@android:color/white" /> <item android:state_pressed="true" android:color="@android:color/white" /> <item android:color="@color/lighter_orange" /> </selector> 

and I use it in the line layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="20dp" android:gravity="left|center" android:layout_width="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="@color/list_bg"> <ImageView android:id="@+id/avatar" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginTop="10dp" android:layout_alignParentLeft="true" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_toRightOf="@+id/avatar" android:layout_marginLeft="10dp" android:layout_marginTop="5dp"> <TextView android:id="@+id/listitem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:textColor="@drawable/textblack_selected" android:textSize="18dp" android:textStyle="bold" /> <TextView android:id="@+id/listitem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:textColor="@drawable/textorange_selected" /> </LinearLayout> <ImageView android:id="@+id/arrowImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:layout_alignParentRight="true" android:src="@drawable/arrow_selector" /> </RelativeLayout> 

The problem is that it never goes into android:state_enabled="false" (this should be the moment the item is disabled, and I set the text color to gray).

To disable elements, I use this code in my adapter:

 @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { return position < 1; } 

Where is my mistake?

+4
source share
2 answers

The selector uses the value isEnabled() in the view to which it is assigned, in this case RelativeLayout . It will not call isEnabled() on the Adapter .

You will need a code, for example:

 public View getView(int position, View convertView, ViewGroup parent) { RelativeLayout v = (RelativeLayout)LayoutInflater.from(parent.getContext()) .inflate(R.layout.listitem_relativelayout, null); if(postion < 1) v.setEnabled(false); 
+2
source

The problem is that the included state returned by isEnabled() is not automatically forwarded to all child views of the list item. You need to make a view.setEnabled() call at the end of your getView() adapter to make your selector functional.

Also, make sure you pass the state down to descendants by specifying android:duplicateParentState="true" in the TextView itself, as well as on your LinearLayout wrapper.

+1
source

All Articles