0

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

Stranger B.
  • 9,004
  • 21
  • 71
  • 108
  • duplicate of http://stackoverflow.com/questions/21253371/android-listview-how-to-change-background-color-of-manually-selected-item – CSmith Mar 10 '15 at 14:32

1 Answers1

2

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>
avjr
  • 73
  • 11
  • That's nice ! is there any other animation which I can add to the transition between both Activities ? – Stranger B. Mar 10 '15 at 15:00
  • i never worked with animation on transitions, but maybe this link helps: https://developer.android.com/training/material/animations.html – avjr Mar 10 '15 at 16:33