0

This is my code for the list view:

<?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"
    android:background="@color/darkgrey">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

This is the list selector:

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

  <item android:drawable="@drawable/gradient_bg" />
</selector>

My goal is to add a background to the list items: initial (no click happened and no focus) -> a grey background. When pressed -> blue background. When released -> grey background.

But the initial background is not showing up. The grey background does not show up when opening the app and when you havent clicked an item yet. Someone knows how to fix this?

The java part:

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Index extends Activity {

    public static final Object TITLE = "title";
    public static final Object SUBTITLE = "subtitle";
    public static final Object THUMBNAIL = "thumbnail";
    protected static final String POSITION = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        // Back actionbar icon enable
        //ActionBar actionBar = getActionBar();
        //actionBar.setDisplayHomeAsUpEnabled(true);

        buildMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    //Builds the menu for listview
    public void buildMenu(){
        ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
        //Arrays for info
        String[] menuTitleArray = {"Updates", "Gallerij"}; 
        String[] menuSubtitleArray = {"Bekijk updates", "Bekijk foto's en geef reacties", "Bekijk de updates"};
        String[] menuThumbnailArray = {"updates", "gallery"};
        for(int i=0; i < menuTitleArray.length; i++){
            // Build Hashmap for the item
            HashMap<String, String> item = new HashMap<String, String>();
            item.put((String) TITLE, menuTitleArray[i]);
            item.put((String) SUBTITLE, menuSubtitleArray[i]);
            item.put((String) THUMBNAIL, menuThumbnailArray[i]);
            menu.add(item);
        }


        // Add adapter to the list
        MenuAdapter adapter = new MenuAdapter(this, menu);
        ListView list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);



        // Initialize the click event
        list.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                switch(position){
                case 0:
                    Intent intent = new Intent(Index.this, Updates.class);
                    startActivity(intent);
                }
            }
        });

    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • http://stackoverflow.com/questions/13474760/listselector-color-after-pressed-an-item I had the same issue and found the solution from the above link, I used the solution by Shikha Shah – user1702512 Aug 01 '13 at 11:34

3 Answers3

1

I guess it's because the layout xml of your list row has it's own background set. And then your listSelector is successful set but get covered:)

Bolton
  • 2,226
  • 3
  • 25
  • 29
  • is there a way to turn the background set off? –  Feb 27 '13 at 15:06
  • if you want to use listSelector, then set background of your row layout to @android:color/transparent – Bolton Feb 27 '13 at 15:26
  • :S then try to set listSelector to @android:color/transparent and use the custom selector as background of your list row. – Bolton Feb 27 '13 at 15:39
  • Really strange, could you post your Fragment or Activity that contain the listview? – Bolton Feb 27 '13 at 15:46
  • the java part, not the xml – Bolton Feb 27 '13 at 15:54
  • This is OK, Then the problem should be in MenuAdapter and it's layout~~ – Bolton Feb 27 '13 at 16:01
  • menuadapter has no layout, the only other layout is the layout for the listrows –  Feb 27 '13 at 16:06
  • here is the .java: http://pastebin.com/2J7xPD9T and the listrow: http://pastebin.com/4xabKUXg –  Feb 27 '13 at 16:09
  • vi = inflater.inflate(R.layout.list_row, null); R.layout.list_row is the real view that was shown as the list row, change the background of it to your custom selector – Bolton Feb 27 '13 at 16:13
  • it works with vi.setBackgroundResource(activity.getResources().getIdentifier("list_selector", "drawable", Index.class.getPackage().getName())); Thanks –  Feb 27 '13 at 16:20
0

Use your android:listSelector="@drawable/list_selector" in your custom item layout you used for ListView items.

Like you have your ListView here

<?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"
   android:background="@color/darkgrey">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
         />

</LinearLayout>

and it is using following layout

<?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"
    android:listSelector="@drawable/list_selector">

</LinearLayout>

Edit

You can also use selector like

<item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

I guess you wish to add selector on list row, not on ListView :) Here is the link for you.

Community
  • 1
  • 1