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);
}
}
});
}
}