1

i want to change the linear layout background of the selected item in list view. I have used selector for this. But it is changing background only once & then again goto the default state.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android.com/apk/res/android">;
  <item android:state_pressed="true" android:drawable="@drawable/box_2" />
  <item android:state_focused="true" android:drawable="@drawable/box_2" />
  <!-- focused -->
  <item android:state_hovered="true" android:drawable="@drawable/box_2" />
  <!-- hovered -->
  <item android:state_selected="true" android:drawable="@drawable/box_2" />
  <!-- selected -->
  <item android:drawable="@drawable/box" />
  <!-- default -->
</selector>
IvanH
  • 5,039
  • 14
  • 60
  • 81

2 Answers2

1

If you want to change LinearLayout background permanently, you have to do like so:

    //lay is your layout!
    //if you create a file color.xml in res/values
    lay.setBackgroundColor(R.color.blue);
    //different way
    lay.setBackgroundResource(getResources().getColor(R.color.blue));
    //or if you want to set a drawable as background
    lay.setBackgroundResource(getResources().getDrawable(/*your drawable*/));

Use this code inside the OnClickListene of your layout. The code that you're using change background only on click/focus. Hope it helps ya!

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
  • I have to change the layout of the list item. So should i do this layout change inside onitemclicklistener? It will work tehre? – user3012173 Nov 20 '13 at 08:49
  • if you want to change the background when you click on a list item, yes. – MikeKeepsOnShine Nov 20 '13 at 09:03
  • i have used this LinearLayout ll = (LinearLayout)view.findViewById(R.id.ll_category); ll.setBackgroundResource(R.drawable.box_2); But its not working. Here Linear layout is the layout of the list item. – user3012173 Nov 20 '13 at 09:07
  • could you post the class where you do this? – MikeKeepsOnShine Nov 20 '13 at 09:17
  • categoryLV.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) { LinearLayout ll = (LinearLayout)arg1.findViewById(R.id.ll_category); ll.setBackgroundResource(R.drawable.box_2); categoryListAdapter.notifyDataSetChanged(); } – user3012173 Nov 20 '13 at 09:20
  • @user3012173 Did u try my coding? Please use it. In list view i face this problem already. when i change the child views in focused elements that automatically views change into default and then changed elements are affected in another elements. Tell me ,Is this ur problem or else .... – Venkatesh Nov 20 '13 at 09:49
0

I think this problems comes on while scroll the list, is it? Try this

     //  Use ScrollView instead of ListView
         Product_Conttainer=(ScrollView)ConvertView.findViewById(R.id.loadProducts);
         Product_Conttainer.addView(SetScrollList());
         }

         public LinearLayout SetScrollList()
      {
           for(Index=0;Index<list_order.size();Index++)
            {
                View assignedView=ScrollViewSet();
                csLayout.addView(assignedView);
                       }
             return csLayout;
            }

         public View ScrollViewSet()
        {


             View csview  =LayoutInflater.from(getApplicationContext()).inflate(R.layout.rowitemdisp, null);

    final RelativeLayout containerlayout = (RelativeLayout)csview.findViewById(R.id.viewContainer);
        containerlayout.setOnLongClickListener(new OnLongClickListener()
    {

    });
            }

OR ELSE Try this link

Android-Listview items background color changing when scrolling

Community
  • 1
  • 1
Venkatesh
  • 144
  • 1
  • 18