Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Spinner Adapter

Tags:

android

I wanted to apply a custom font to my spinner. The only way i found out is that to create a custom adapter. Here is my code

    private class CustomAdapter extends ArrayAdapter {      private Context context;     private List<CharSequence> itemList;     public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {          super(context, textViewResourceId);         this.context=context;         this.itemList=itemList;     }      public TextView getView(int position, View convertView, ViewGroup parent) {          TextView v = (TextView) super                 .getView(position, convertView, parent);         Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),                 "fonts/gilsanslight.otf");         v.setTypeface(myTypeFace);         v.setText(itemList.get(position));         return v;     }      public TextView getDropDownView(int position, View convertView,             ViewGroup parent) {          TextView v = (TextView) super                 .getView(position, convertView, parent);         Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),                 "fonts/gilsanslight.otf");         v.setTypeface(myTypeFace);         v.setText(itemList.get(position));         return v;     }  } 

Then i use

List<CharSequence> itemList = new ArrayList<CharSequence>(             Arrays.asList(items));      mySpinnerArrayAdapter = new   CustomAdapter(context,android.R.layout.simple_spinner_item,itemList);      spinner.setAdapter(mySpinnerArrayAdapter); 

After doing this, my adapter is empty. Can anyone please help me ? The items contains a list of countries.

Kind Regards,

like image 455
AndroidDev Avatar asked Sep 26 '12 10:09

AndroidDev


People also ask

How can I use more than one spinner in android?

Clearly each of your adapters need to adapt a different set of String s, so you need to create an ArrayAdapter for each Spinner . A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?>

How do I add a spinner to my toolbar?

Adding spinner to app bar/ toolbar is very simple, you just need to create a XML file in res/menu/ folder and add a item like your over flow menu and spinner widget as item actionViewClass, rest in your java code. Spinner can be added to android actionbar/toolbar with many ways.


2 Answers

Try

public View getView(int position, View convertView, ViewGroup parent) {      LayoutInflater inflater = getLayoutInflater();             View row = inflater.inflate(yourRowlayout, parent,                     false);        TextView make = (TextView) row.findViewById(R.id.Make);         Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),                 "fonts/gilsanslight.otf");         v.setTypeface(myTypeFace);         v.setText(itemList.get(position));         return row;     }   public View getDropDownView(int position, View convertView, ViewGroup parent) {          LayoutInflater inflater = getLayoutInflater();                 View row = inflater.inflate(yourRowlayout, parent,                         false);            TextView make = (TextView) row.findViewById(R.id.Make);             Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),                     "fonts/gilsanslight.otf");             v.setTypeface(myTypeFace);             v.setText(itemList.get(position));             return row;         } 
like image 156
mukesh Avatar answered Nov 01 '22 03:11

mukesh


Pass itemList as parameter in super class constructor

public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {      super(context, textViewResourceId, itemList);     this.context=context;     this.itemList=itemList; } 
like image 29
Vipin Sharma Avatar answered Nov 01 '22 04:11

Vipin Sharma