0

I implemented my own custom Adapter spinner as you can see in the code. everything is working except that that the image is attached from the left side on each value instead attached only to the selected value. in the following image this is what i get: enter image description here

and this is my code:

public class MyAdapter extends ArrayAdapter<String> {

    private Context context;
    private String[] values;

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
        context = ctx;
        values = objects;
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                false);
        TextView subSpinner = (TextView) mySpinner.findViewById(R.id.sub_text_seen);
        subSpinner.setText(values[position]);
        ImageView right_icon = (ImageView) mySpinner.findViewById(R.id.left_pic);


        return mySpinner;
    }
}

this is the initalize of the adapter:

spinnerduration.setAdapter(new MyAdapter(this, R.layout.custom_spinner, weeks_array));
    spinnerduration.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            /*TextView selectedText = (TextView) parent.getChildAt(0);
            if (selectedText != null) {
                selectedText.setTextColor(Color.WHITE);
            }*/
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            /*TextView selectedText = (TextView) parent.getChildAt(0);
            if (selectedText != null) {
                selectedText.setTextColor(Color.WHITE);
            }*/
        }
    });
Matan Tubul
  • 774
  • 3
  • 11
  • 33

1 Answers1

1

You need to iterate visible items and if they are not selected you should erase image:

selectedItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

if it's selected you need to set your triangle drawable:

selectedItem.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
Access Denied
  • 8,723
  • 4
  • 42
  • 72