listview normal and hover background style in android
Asked Answered
L

4

4

this is my coding part:

listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">


<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:divider="#FFFFFF"
    android:dividerHeight="7dp"
     android:layout_weight="1"
    android:listSelector="@drawable/list_selector" />


 </LinearLayout>    

list_selector.xml file:

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

 <item 
android:state_selected="false"
android:state_pressed="false" 

android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true" 
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false" 
android:drawable="@drawable/gradient_bg_hover" />
</selector>

gradient_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
  android:startColor="#e8eef4"
  android:centerColor="#fdfdfd"
  android:endColor="#d3dfe8"
  android:angle="270" />
        <stroke android:width="1dp" android:color="#b7ccdd" />

    <padding android:left="6dp" android:top="6dp" android:right="6dp"
            android:bottom="6dp" />
     <margin android:left="15dp" android:top="15dp" android:right="15dp"
            android:bottom="15dp" />
        <corners android:radius="4dp" />
    </shape>

gradient_bg_hover.xml:

<?xml version="1.0" encoding="utf-8"?>

  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
  android:startColor="#ffe181"
  android:centerColor="#f5cd4a"
  android:endColor="#e8c34e"
  android:angle="270" />
        <stroke android:width="1dp" android:color="#e3b52c" />

    <padding android:left="6dp" android:top="6dp" android:right="6dp"
            android:bottom="6dp" />
     <margin android:left="15dp" android:top="15dp" android:right="15dp"
            android:bottom="15dp" />
        <corners android:radius="4dp" />
     </shape>

Here i got the o/p : enter image description here

But i wish to need the o/p like: enter image description here

please compare the hover state on both images and give me solution for this..

Lindi answered 26/10, 2012 at 4:49 Comment(2)
set the background of image on hover!Odontograph
refer my code.i have set the background image on hove state.see my gradient_bg_hover.xml fileLindi
A
6

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#FFFFFF"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true" 
    android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg_hover" />
</selector>

gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!--  Gradient Bg for listrow -->
  <gradient
      android:startColor="#f1f1f2"
      android:centerColor="#e7e7e8"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

gradient_bg_hover.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!-- Gradient BgColor for listrow Selected -->
  <gradient
      android:startColor="#18d7e5"
      android:centerColor="#16cedb"
      android:endColor="#09adb9"
      android:angle="270" />
</shape>
Attlee answered 26/10, 2012 at 8:57 Comment(0)
G
2

you are not using focused state try using the below code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg_hover" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@drawable/gradient_bg_hover" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg_hover" />
</selector>
Georgettegeorgi answered 26/10, 2012 at 8:55 Comment(1)
i have to used your code.now also am getting my current output only.Lindi
E
0

To late but for other :)

you need to set the selector for your listview to transparent color

listView.setSelector(R.color.transparent);

In color.xml just add the following

<color name="transparent">#00000000</color>
Emptyheaded answered 7/11, 2014 at 16:4 Comment(0)
D
0

Remove Padding from gradient_bg_hover.xml & gradient_bg.xml

And Instead Put Padding in ListView Like:

<ListView
    android:id="@+id/Sales_List"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    **android:paddingLeft="6dp"
    android:paddingTop="6dp"
    android:paddingRight="6dp"
    android:paddingBottom="6dp"**
    android:dividerHeight="7dp"
    android:divider="@android:color/transparent"
    android:listSelector="@drawable/listviewselect">

It Should Work

Desiderate answered 24/2, 2016 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.