views:

72

answers:

2

Hi folks,

I am an Android newbie trying to learn the UI side of things and it's doing my head in. Here's what I have right now:

breeds_listing.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ListView android:id="@+id/breedsListView"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:dividerHeight="0px" android:divider="#00000000"></ListView>
</LinearLayout>

breeds_listing_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@drawable/list_item_background"
    android:orientation="horizontal" >
    <TextView android:id="@+id/allbreeds_single_item"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:gravity="center_horizontal"></TextView>
</LinearLayout>

list_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <solid android:color="#043402" />
    <stroke android:width="3dp" color="#ffff8080" />
    <corners android:radius="8dp" />
    <padding android:left="10dp" android:top="10dp" android:right="10dp"
        android:bottom="10dp" />
</shape>

What I am struggling to figure out is how do I style the selected item? At the moment the selected item has a ghastly orange background which, under the rounded green rectangle, gives an orange outline effect. I'm sure I have not done things in line with best practices so any suggestions or improvements would be greatly appreciated.

Many thanks, D.

+1  A: 

I do this :

 <selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

   <item android:state_pressed="true">
       <shape  >
         <solid android:color="#929292" />
       </shape>
   </item>



   <item>
     <shape  >
        <solid android:color="#FFFFFF" />
     </shape>
   </item>

 </selector>

So you can use this property android:state_pressed="true"

In my example, the cell background will only be white, and grey when pressed.

I hope it helps!

ps: your accept rate isn't high at all... It doesn't encourage people to answer your questions ;)

William Remacle
Yup - I'm terrible at forgetting to vote up or accept answers! Will definitely work on that...have just gone back through my questions and marked the ones that worked for me. Thanks for the gentle reminder :o)
DanyW
Yippeeeee! I got it to work - thanks for getting me over the mind block! Dunno why I didn't think of this since we do something very similar in the WPF world of my day job! It was just a matter of finding the right state to use :o)
DanyW
Happy to hear it worked for you ^^
William Remacle
Ok, I have now done this for the background - the next question I can't quite grasp is how do I specify style for the text? Do I need to create another XML file with selector to specify the text properties?
DanyW
Yes, another XML file with selector. You have an example here: http://developer.android.com/guide/topics/resources/color-list-resource.html
Edi
Yes ... I did like this too.
William Remacle
+1  A: 

You have to use a StateListDrawable.

You can find an example for ListView items here.

If you want a certain color/drawable when the item is selected, use android:state_focused="true".

Edi