Skip to content Skip to sidebar Skip to footer

How To Change Background Color When Click On Horizontal Listview Items In Android

How to change background color when clicking on Horizontal Listview items in Android So that users will know where they clicked. Below is the code which I got from some internet si

Solution 1:

For example in this implementation you can use listSelector property in xml. Check out maybe yours also supports similar property or use the one I linked above.

Solution 2:

public View selectedRow;//declare this in class

Add this code in onitemSelectListner

if (selectedRow != null) {
    selectedRow.setBackgroundResource(color.transparent);                   
}
selectedRow = view; //view is the onitemSelectListner View
view.setBackgroundResource(R.drawable.list_background); 

this will work 3.0 and above version..

Solution 3:

add this onclick listener in getview method..

list.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @OverridepublicvoidonItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                for(int j=0;j<20;j++)
                {
                    test[j]=0;
                }


                for (intk=0; k < mAdapter.getCount(); k++) {
                    Viewitem= list.getChildAt(k);
                    if (item != null) {
                        test[k]=0;
                        item.setBackgroundColor(Color.WHITE);
                    }
                    view.setBackgroundColor(Color.BLUE);
                    test[i]=1;

                }
            }

test is a dummy array, use it out of onclicklistner to keep the selected even when view is changed.. like this..

   if (test[position]==1) {             
            relativeLayout.setBackgroundColor(Color.BLUE);
        }
        if(test[position]==0)
            relativeLayout.setBackgroundColor(Color.WHITE);

Post a Comment for "How To Change Background Color When Click On Horizontal Listview Items In Android"