Android Custom spinner .

Android Custom spinner .

Step -1 : Create new Adapter .

public class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
 
        private final Context context;
        private ArrayList<String> asr;
 
        public CustomSpinnerAdapter(Context context1, ArrayList<String> asr) {
            this.context = context1;
            this.asr=asr;

        }
 
 
 
        public int getCount()
        {
            return asr.size();
        }
 
        public Object getItem(int i)
        {
            return asr.get(i);
        }
 
        public long getItemId(int i)
        {
            return (long)i;
        }
 
 
 
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            TextView txt = new TextView(context);
            txt.setPadding(16, 16, 16, 16);
            txt.setTextSize(18);
            txt.setGravity(Gravity.CENTER_VERTICAL);
            txt.setText(asr.get(position));
            txt.setTextColor(Color.parseColor("#000000"));
            return  txt;
        }
 
        public View getView(int i, View view, ViewGroup viewgroup) {
            TextView txt = new TextView(context);
            txt.setGravity(Gravity.CENTER);
            txt.setPadding(16, 16, 16, 16);
            txt.setTextSize(16);
            txt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_down, 0);
            txt.setText(asr.get(i));
            txt.setTextColor(Color.parseColor("#000000"));
            return  txt;
        }
 
    }

2. Create new drawable

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="@android:color/white"/>


    <corners android:radius="4dp"></corners>
<stroke android:color="#cccccc" android:width="1dp"/>

</shape>

3. Create spinnerview in xml layout .

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="@dimen/count_40dp"
    android:layout_marginLeft="@dimen/count_5dp"
    android:layout_marginRight="@dimen/count_5dp"
    android:layout_marginTop="@dimen/count_10dp">

    <Spinner
        android:id="@+id/gender"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v7.widget.CardView>

4 . Call the custom Spinner in Activity

 ArrayList<String> languages = new ArrayList<String>();
    languages.add("Male");
    languages.add("Female");
    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages);

   CustomSpinnerAdapter customSpinnerAdapter=new CustomSpinnerAdapter(mContext,languages);
   genderSpinner.setAdapter(dataAdapter );
    genderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String item = parent.getItemAtPosition(position).toString();

            genderString = item;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

5 . Run it . That’s all .

Leave a Reply

Your email address will not be published. Required fields are marked *