Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Items in lists not turning orange when clicked?

I have a ListView in my app's code, which when clicked uses an AdapterView.OnItemClickListener to detect clicks. The problem is, when I click on an item , that item's background turns to white, instead of the default orange. Like this:enter image description here

Also, when I dont use AdapterView, the clicked items turn orange without any problem. How do I make the clicked item's background orange again?

EDIT:

layout of list: main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" 
    android:background="#00000000">


        <!-- ListView (grid_items) -->

        <ListView android:id="@+id/listview"
            android:layout_height="fill_parent"
            android:textSize="15px"
            android:layout_width="fill_parent"
            android:background="#00000000">
        </ListView>

</RelativeLayout>

onCreate():

public void onCreate(Bundle savedInstanceState) {try{
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);
    lv= (ListView)findViewById(R.id.listview);
    lv.setBackgroundColor(Color.TRANSPARENT);
    lv.setCacheColorHint(Color.TRANSPARENT);
    //......calculations
    for(int q = 0; q <v; q++){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("col_1", array[q]);
        fillMaps.add(map);
        lv.setOnItemClickListener(onListClick);
    }
    //......calculations
    }

AdapterView:

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{

lv.setBackgroundColor(Color.TRANSPARENT);
lv.setCacheColorHint(Color.TRANSPARENT);
//.....calculations
}

Custom theme being used:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>
like image 602
vergil corleone Avatar asked Jun 02 '13 18:06

vergil corleone


1 Answers

The best way to customize list view items is to create them in an xml file and then, using a BaseAdapter or whatever kind of list adapter, inflate that .xml layout and fill up with your data. In order to customize a highlight animation, all you need is to create a state Drawable for the different states of an item.

So you go to a "New xml file" -> "resource type drawable" -> "shape" name it and finish you'll see this piece of code:

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


</shape>

Let's create a background for a ListItem pressed state:

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

    <solid android:color="#86d47f" this is your custom color />  

</shape>

and non-pressed state is another file:

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

    <solid android:color="#00000000" />

</shape>

then a list-selector file that will be used as a background of a listview item: go to "new xml file"->"drawable"->"list selector" name it as "item_background"->finish

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"  android:exitFadeDuration="300"  !!this is the fade duration time for your animation>

    <item android:drawable="@drawable/bg_drawable" android:state_pressed="true"></item>
    <item android:drawable="@drawable/transparend"></item>

</selector>

then create an xml file for an item, customize it as you want, but for the main layout set

android:background="@drawable/item_background"

here you go, everything works perfect... here is my adapter class:

public class ListAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;

public ListAdapter(Context context, int textViewResourceId,
        List<String> objects) {
    super(context, textViewResourceId, objects);
    inflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String str_value = this.getItem(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.textView1);
    tv.setText(str_value);
    return convertView;
}

}

and here you are, supposed you'll mark it as an answer.. list

like image 135
Alex Avatar answered Nov 11 '22 11:11

Alex