n4h1n
n4h1n

Reputation: 357

Apply gradient to rows in listview in android

i've a list view with different colors depending on if it's an even or odd row:

My custom adapter:

 if ( position % 2 == 0)
        convertView.setBackgroundResource(R.layout.listview_selector_even);
      else
          convertView.setBackgroundResource(R.layout.listview_selector_odd);

list_selector_even:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >        
      <item android:drawable="@layout/even_row" android:state_enabled="true"/>
      <item android:drawable="@layout/even_row" android:state_pressed="true"/>
      <item android:drawable="@layout/even_row" android:state_focused="true"/>
</selector>

list_selector_odd:

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item android:drawable="@layout/odd_row" android:state_enabled="true"/>
      <item android:drawable="@layout/odd_row" android:state_pressed="true"/>
      <item android:drawable="@layout/odd_row" android:state_focused="true"/>
  </selector>

even_row:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#FFFFFF"/>  
   <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />       
</shape>

odd_row:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F0F0F0"/>
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>

I'm a little bit lost because i cant get the even and odd rows colors working with the gradient effect. For the preessed effect gradient i've the next code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
      <gradient
        android:startColor="#3E5260"
        android:endColor="#3E5260"
        android:angle="270" />
    </shape>
 </item>
</selector>

Thanks for the help!

Upvotes: 2

Views: 2886

Answers (1)

MalaKa
MalaKa

Reputation: 3794

You already check for the state in the selector, so for the gradient, you should do this instead:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
  <gradient
    android:startColor="#3E5260"
    android:endColor="#3E5260"
    android:angle="270" />
</shape>

Also, either change the selectors to following and define a file for each selector item, or move the selector with all available states to the odd_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
  <item android:drawable="@layout/odd_row_pressed" android:state_pressed="true"/>
  <item android:drawable="@layout/odd_row_focused" android:state_focused="true"/>
  <item android:drawable="@layout/odd_row_enabled"/>
</selector>

This version ensures that odd_row_enabled.xml is chosen if the item is not pressed and not focused. If you want another layout if the item is not enabled, you have to add one more line (before the default line). Also, it ensures that odd_row_pressed.xml is chosen in case it is pressed and enabled, because the <item> for the state pressed comes before the other lines.

Upvotes: 2

Related Questions