Garry
Garry

Reputation: 1271

ListView item color in XML file

I am using the Xamarin sample at the following link: Part 3 - Customizing a ListView's Appearance

The sample code is from the following file: CustomRowView.zip

Here is a screenshot of my running code: Emulator screenshot

The background color of the ListView items are in yellow.

May I please have some help to change this color to be the default color, or to choose this color? I am not sure where to find this in code. I have looked in the XML layout files, but cannot find the correct item.

In the RelativeLayout, there is a reference to the following:

android:background="@drawable/CustomSelector"

And this is the contents of the file:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false"         
        android:state_selected="false"         
        android:drawable="@color/cellback" />
  <item android:state_pressed="true" >
    <shape>
      <gradient
      android:startColor="#E88A93"
      android:endColor="#E88A93"
      android:angle="270" />
    </shape>
  </item>

  <item android:state_selected="true" 
        android:state_pressed="false"         
        android:drawable="@color/cellback" />
</selector>

Is this code relevant to the question?

Thanks in advance

Upvotes: 0

Views: 1832

Answers (4)

Seli17
Seli17

Reputation: 1

I never get it yellow, which should come after pressing an item in the list. (I changed your sample to yellow for state_pressed=true. Tks Seli

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@color/light_grey" android:state_pressed="false"        android:state_selected="false"/>
  <item android:state_pressed="true">
    <shape>
      <gradient android:angle="270" android:endColor="#E88A93" android:startColor="#E88A93" />
    </shape>
  </item>
  <item android:drawable="@color/yellow" android:state_pressed="false"  android:state_selected="true"/>

</selector>

Upvotes: 0

Radheshyam Singh
Radheshyam Singh

Reputation: 479

Create a color.xml in your values file and defines your color(which color you wnt to set to your listview ite) here

<?xml version="1.0" encoding="utf-8"?>
 <resources>
    <color name="magenta">#FF00FF</color>
    <color name="yellow">#FFFF00</color>
    <color name="light_grey">#ffb9b8bb</color>
</resources>

and then use these colors in your custom selector

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

     <item android:drawable="@color/light_grey" android:state_pressed="false"        android:state_selected="false"/>
     <item android:state_pressed="true"><shape>
        <gradient android:angle="270" android:endColor="#E88A93" android:startColor="#E88A93" />
      </shape></item>
   <item android:drawable="@color/light_grey" android:state_pressed="false"  android:state_selected="true"/>

  </selector>

Upvotes: 0

showyoumuck
showyoumuck

Reputation: 39

Your ListView is using CustomView.axml for each row. in CustomView.axml parent element (RelativeLayout) is using CustomSelector.xml from Drawable folder in android:background attribute. In this file you can change colors of three states (pressed, pressed & selected, none) as you wish.

Upvotes: 0

J Jiang
J Jiang

Reputation: 1512

The "yello background color" is the custom listview item's background color. In the toturial you referenced, you can find it in "/Resources/Layout/CustomView.axml" :

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="#FFDAFF7F"
   android:padding="8dp">
 <LinearLayout android:id="@+id/Text"
   android:orientation="vertical"

...    
...

   android:layout_height="48dp"
   android:padding="5dp"
   android:src="@drawable/icon"
   android:layout_alignParentRight="true"
   />
</RelativeLayout >

the android:background="#FFDAFF7F" is what you want to modify.

Upvotes: 1

Related Questions