Question

I am currently trying to implement a ListView in Android that features rows with an icon and a describing text. When the user presses a row, the background color should change and the icon should be replaced as shown in the image below.normal and pressed state

Changing the background is no problem using a selector. But i just can't figure out a simple way to change the icon. What i tried was adding 2 icons overlapping each other:

<RelativeLayout
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/general_list_item_icon"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerInside" />

    <ImageView
        android:id="@+id/general_list_item_icon_active"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone" />
</RelativeLayout>

Then i put an drawable in each ImageView and want to switch the visibility when the user presses the row. Is there any possibility to do this using a selector similar to the one for the background?

Also: Since the ListView is filled dynamically, using a selector with static drawable references does not work.

Was it helpful?

Solution

You can use setChoiceMode

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

call this method before calling set adapter. And override the getview method of your adapter. Sample code.

public View getView(int position, View convertView,
                    ViewGroup parent) {
                if(convertView!=null){
                    ImageView img = (ImageView)convertView.findViewById(R.id.imageView1);
                    if(mylist.isItemChecked(position)){
                        convertView.setBackgroundColor(Color.WHITE);// here you can set any color.
                        img.setImageResource(R.drawable.img1);//img1 is stored in your rawable folder.
                    }else{
                        convertView.setBackgroundColor(0);
                        img.setImageResource(R.drawable.img2);
                    }
                }
                return super.getView(position, convertView, parent);
            }

I hope this will give you the solution what you want. All the best. Thank you.

OTHER TIPS

This is my solution that worked for me :

public void setNavDrawerItemNormal()
    {
        for (int i=0; i< mDrawerList.getChildCount(); i++)
        {
            View v = mDrawerList.getChildAt(i);
            ImageView icon = ((ImageView) v.findViewById(R.id.icon));
            icon.setImageResource(navMenuIcons.getResourceId(0, -1));
        }
    }



private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            //adapter.notifyDataSetChanged();

            setNavDrawerItemNormal();
            if(view!=null) {
                ImageView img = (ImageView) view.findViewById(R.id.icon);
                img.setImageResource(R.drawable.abs__ic_go);//img1 is stored in your rawable folder.
            }


            // display view for selected nav drawer item
            displayView(position);




        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top