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 e
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;
}
The simplest, I think :)
List<String> listOfItems = getListOfItems(); // returns ArrayList<String>
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfItems);
targetSpinner.setAdapter(spinnerAdapter);
Ok, to simply put a list of string in a spinner shouldn't force us to implement an Adapter. That's a code blot and getting a bit pattern crazy, I think.
Trick is the simple_spinner_item id - damn, I like the R.id mechanism but this isn't intuitive from the docs.
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;
}
Try this out
Inside your Layout File:
<Spinner
android:id="@+id/spinnerview"
android:layout_width="180dp"
android:layout_height="42dp"
android:layout_marginLeft="105dp"
android:layout_marginTop="45dp"
android:background="@drawable/spinner_back"
android:paddingLeft="5dp"
android:spinnerMode="dropdown"
android:visibility="visible" />
inside your string.xml
:
<string-array name="spinner_array_environtment">
<item>Test</item>
<item>Production</item>
</string-array>
inside you Java File in onCreate()
Method:
spinner_environment = (Spinner) findViewById(R.id.spinnerview);
adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array_environtment, R.layout.spinner);
adapter.setDropDownViewResource(R.layout.spinner);
spinner_environment.setAdapter(adapter);
Make new spinner.xml
file to your layout folder :
inside spinner.xml
file :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#4C4646" />
Thats it!!!
this worked for me (ussing android.R.layout.simple_spinner_dropdown_item):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView checkedTextView = (CheckedTextView) super.getView(position, convertView, parent);
checkedTextView.setText(itemList.get(position));
return checkedTextView;
}
i think it's a better solution because you don't inflate multiple times.