I am developing an Android app in Gujarati language, need to set Gujarati text on a Spinner widget. So, I learned how to set custom fonts on Spinner from HERE.
But the problem is I am getting boxes instead of actual text. My text for spinner comes from string resources. Attaching my code.
public class SpinnerExActivity extends Activity {
Spinner mySpinner;
Typeface myFont;
String[] items;
MyArrayAdapter ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex);
mySpinner = (Spinner) findViewById(R.id.spinner1);
items=getResources().getStringArray(R.array.items);
myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
ma=new MyArrayAdapter(this, R.layout.my_spinner_style);
ma.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
private class MyArrayAdapter extends ArrayAdapter {
public MyArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(myFont);
v.setText(items[position]);
return v;
}
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(myFont);
return v;
}
}
I get boxes when I try this on emulator. But when I try it on my tablet I get nothing.
Any help would be appreciable.
EDIT This Works:: Thanx to this.
public class SpinnerExActivity extends Activity {
Spinner mySpinner;
Typeface myFont;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex);
mySpinner = (Spinner) findViewById(R.id.spinner1);
items=getResources().getStringArray(R.array.items);
myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.my_spinner_style, items) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
((TextView) v).setTypeface(externalFont);
((TextView) v).setText(items[position]);
((TextView) v).setTextColor(Color.BLACK);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
((TextView) v).setTypeface(externalFont);
((TextView) v).setText(items[position]);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
}