Stranger B.
Stranger B.

Reputation: 9364

How to change the color of selected item on ListView using AdapterView

I'm looking on how I can change the color of the selected item on ListView, so I can give the user better way to use my application, so when clicking on a ListView item, the color of the item changes, or any cool animation.

I'm using an adapterView for my Listview : here is the code :

public class adapterq extends ArrayAdapter<Questionaire> {
Bitmap image;


public adapterq(Context context, ArrayList<Questionaire> questionaires) {
   super(context, 0, questionaires);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
   
   final Questionaire c = getItem(position);    
   
   View  convertView2;
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.customquest, parent, false);
      convertView2 = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
   }else{
    convertView2 = (View) convertView.getTag();
   }




   TextView q = (TextView) convertView.findViewById(R.id.textView1);
   final EditText name =     (EditText) convertView2.findViewById(R.id.editText1);


   q.setText(c.getLabel()); 

   convertView.setOnClickListener(new OnClickListener(){
 @Override
     public void onClick(View v) {
      Intent intent = new Intent();
      intent.setClass(getContext(), Questions.class);
      intent.putExtra("name", name.getText().toString());
      intent.putExtra("category", c.getCode());
      getContext().startActivity(intent);  
      
   
    		 

    }

    });
   convertView.setTag(convertView2);
   return convertView;
}

}

Here is a screenshot of my amazing Listview :

enter image description here

Upvotes: 0

Views: 110

Answers (1)

avjr
avjr

Reputation: 73

you can use inside your onClick:

v.setBackgroundResource(R.drawable.yourbackground);

and create yourbackground.xml inside your drawable folder like that:

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

    <gradient
        android:angle="-270"
        android:endColor="#781704"
        android:startColor="#A61E03" />

</shape>

Upvotes: 2

Related Questions