Custom Spinner
Here I have implemented custom spinner example using Model class.
Here is my activity_main.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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.spinnercustom.MainActivity"> <Spinner
android:id="@+id/sprCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </RelativeLayout>
Now we need to create custom layout for spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tvCountryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
After that we have to create model class for spinner.
public class SpinnerModel implements Serializable { private String strCountryName; public String getStrCountryName() { return strCountryName; } public void setStrCountryName(String strCountryName) { this.strCountryName = strCountryName; } }
and the code of MainActivity is given below.
public class MainActivity extends AppCompatActivity { private Spinner sprCountry; private ArrayList<SpinnerModel> alCountry; private SpinnerModel model; private SpinnerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sprCountry = (Spinner) findViewById(R.id.sprCountry); alCountry = new ArrayList<>(); model = new SpinnerModel(); model.setStrCountryName("Select Coutry"); alCountry.add(model); // set First coutry name
model = new SpinnerModel(); model.setStrCountryName("INDIA"); alCountry.add(model); model = new SpinnerModel(); model.setStrCountryName("USA"); alCountry.add(model); model = new SpinnerModel(); model.setStrCountryName("CANADA"); alCountry.add(model); adapter = new SpinnerAdapter(MainActivity.this,alCountry); sprCountry.setAdapter(adapter); sprCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { Log.e("You select ","=====> "+alCountry.get(i).getStrCountryName()); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } }
and finally I adapter of spinner is
public class SpinnerAdapter extends BaseAdapter { private ArrayList<SpinnerModel> alCountry; private Context context; private LayoutInflater inflater; public SpinnerAdapter(MainActivity mainActivity, ArrayList<SpinnerModel> alCountry) { context= mainActivity; this.alCountry = alCountry; } @Override public int getCount() { return alCountry.size(); } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } class Holder{ private TextView tvCountryName; } @Override public View getView(int i, View convertView, ViewGroup viewGroup) { View myView = null; try { Holder holder; myView = convertView; if (myView == null) { inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); myView = inflater.inflate(R.layout.spinner_layout, null); holder = new Holder(); holder.tvCountryName = (TextView) myView.findViewById(R.id.tvCountryName); myView.setTag(holder); } else { holder = (Holder) myView.getTag(); } holder.tvCountryName.setText(alCountry.get(i).getStrCountryName()); } catch (Exception e) { e.printStackTrace(); } return myView; } // Below code is used to hide first item.
@Overridepublic View getDropDownView(int position, View convertView, ViewGroup parent) { View v = null; if (position == intHideIndex) { TextView tv = new TextView(context); tv.setVisibility(View.GONE); tv.setHeight(0); v = tv; v.setVisibility(View.GONE); } else v = super.getDropDownView(position, null, parent); return v; }}