Search code examples
androidandroid-spinner

How to set visibility of imageview to VISIBLE of the item selected of Spinner


I want to set the visibility to VISIBLE when the item is selected and GONE for the items which are not selected.

Currently I have a Adapter class with the following code below:-

   public class FruitAdapter extends BaseAdapter {
    private Context context;
    private List<Fruit> fruitList;

    public FruitAdapter(Context context, List<Fruit> fruitList) {
        this.context = context;
        this.fruitList = fruitList;
    }

    @Override
    public int getCount() {
        return fruitList != null ? fruitList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View rootView = LayoutInflater.from(context)
                .inflate(R.layout.item_fruit, viewGroup, false);

        TextView txtName = rootView.findViewById(R.id.name);
        ImageView image = rootView.findViewById(R.id.image);
        ImageView selected = rootView.findViewById(R.id.selected);

        txtName.setText(fruitList.get(i).getName());
        image.setImageResource(fruitList.get(i).getImage());

        
        return rootView;
    }
}

and the xml file below:-

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:src="@drawable/orange"
        android:layout_marginStart="8dp"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:layout_toRightOf="@+id/image"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"/>

    <ImageView
        android:id="@+id/selected"
        android:layout_width="16dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_height="16dp"
        android:visibility="gone"
        android:src="@drawable/ic_selected"
        android:layout_marginEnd="30dp"/>

</RelativeLayout>

In the MainActivity I can get the position of the item selected. But I cannot pass the value to the adapter to achieve my result.

   public class MainActivity extends AppCompatActivity implements CustomSpinner.OnSpinnerEventsListener{

    private CustomSpinner spinner_fruits;

    private FruitAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner_fruits = findViewById(R.id.spinner_fruits);

        spinner_fruits.setSpinnerEventsListener(this);

        adapter = new FruitAdapter(MainActivity.this, Data.getFruitList());
        spinner_fruits.setAdapter(adapter);
    }

    @Override
    public void onPopupWindowOpened(Spinner spinner) {
        spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit_up));
    }

    @Override
    public void onPopupWindowClosed(Spinner spinner) {
        spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit));
    }

}

and the xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.customspinners.CustomSpinner
        android:id="@+id/spinner_fruits"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:dropDownVerticalOffset="40dp"
        android:layout_margin="16dp"
        android:background="@drawable/bg_spinner_fruit"/>

</RelativeLayout>

Solution

  • You can create an interface in the Adapter class. When your item clicked you trigger it with calling its method. And you can use it in your activity class by implementing this interface

           public class FruitAdapter extends BaseAdapter {
        private Context context;
        private List<Fruit> fruitList;
        private OnClickListener onClick;
    
        public FruitAdapter(Context context, List<Fruit> fruitList, OnClickListener onClick) {
            this.context = context;
            this.fruitList = fruitList;
            this.onClick= onClick;
        }
    
        @Override
        public int getCount() {
            return fruitList != null ? fruitList.size() : 0;
        }
    
        @Override
        public Object getItem(int i) {
            return i;
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View rootView = LayoutInflater.from(context)
                    .inflate(R.layout.item_fruit, viewGroup, false);
    
            TextView txtName = rootView.findViewById(R.id.name);
            ImageView image = rootView.findViewById(R.id.image);
            ImageView selected = rootView.findViewById(R.id.selected);
    
            txtName.setText(fruitList.get(i).getName());
            image.setImageResource(fruitList.get(i).getImage());
              
            rootView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                  onClick.onClick();
                }
              });
            
            return rootView;
        }
    
        interface OnClickListener {
            void onClick();
        }
     }
    
           public class MainActivity extends AppCompatActivity implements CustomSpinner.OnSpinnerEventsListener, FruitAdapter.OnClickListener{
    
        private CustomSpinner spinner_fruits;
    
        private FruitAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            spinner_fruits = findViewById(R.id.spinner_fruits);
    
            spinner_fruits.setSpinnerEventsListener(this);
    
            adapter = new FruitAdapter(MainActivity.this, Data.getFruitList());
            spinner_fruits.setAdapter(adapter);
        }
    
        @Override
        public void onPopupWindowOpened(Spinner spinner) {
            spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit_up));
        }
    
        @Override
        public void onPopupWindowClosed(Spinner spinner) {
            spinner_fruits.setBackground(getResources().getDrawable(R.drawable.bg_spinner_fruit));
        }
      @Override
      public void onClick(){
    
      }
    
    }