How to Set Font Custom Font to Spinner Text Programmatically

How to set font custom font to Spinner text programmatically?

This is what worked for me (using ideas both from CommonsWare's and gsanllorente's answers):

private static class MySpinnerAdapter extends ArrayAdapter<String> {
// Initialise custom font, for example:
Typeface font = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Blambot.otf");

// (In reality I used a manager which caches the Typeface objects)
// Typeface font = FontManager.getInstance().getFont(getContext(), BLAMBOT);

private MySpinnerAdapter(Context context, int resource, List<String> items) {
super(context, resource, items);
}

// Affects default (closed) state of the spinner
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTypeface(font);
return view;
}

// Affects opened state of the spinner
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getDropDownView(position, convertView, parent);
view.setTypeface(font);
return view;
}
}

If you, like me, originally populated the Spinner using ArrayAdapter.createFromResource() and an array resource (as in Spinner documentation), then you'd use MySpinnerAdapter like this:

MySpinnerAdapter<String> adapter = new MySpinnerAdapter(
getContext(),
R.layout.view_spinner_item,
Arrays.asList(getResources().getStringArray(R.array.my_array))
);
spinner.setAdapter(adapter);

Spinner with custom text font and color

Here you can make a custom xml file in layout folder where you can add this:

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:padding="10dp"
android:textStyle="bold" />

And then in your code mention it like this:

val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is mycustom xml file.

And then set the adapter.

Set Custom Font To Spinner

Why

The Spinner is adapter pattern. You are in the wrong place to set the typeface.

This works for EVERY SINGLE textview.

Yes, you can find a TextView in your Activity's layout and configure its typeface, because Activity need the layout to show it content.

The Spinner also have a layout as you can see it is a popup, its content need Adapter to configure, so in the Adapter you can get the TextView and set its typeface.

How

Like @elmorabea has said, you need a custom Adapter and in the GetView method, you need to set the Typeface, below is a simple:

public class MainActivity : Activity
{
Spinner spnMyFriends;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
initView();
}

private void initView()
{
spnMyFriends = FindViewById<Spinner>(Resource.Id.txt_myfriends_addafriend);

List<string> friends = new List<string>
{
"My friends",
"I am following",
};

MyAdapter myAdapter = new MyAdapter(this,friends);
spnMyFriends.Adapter = myAdapter;

}

}

public class MyAdapter : BaseAdapter<string>
{
Context mContext;
readonly LayoutInflater inflater;
List<string> itemList;
public MyAdapter(Context context, List<string> list) {
this.inflater = LayoutInflater.FromContext(context);
this.mContext = context;
this.itemList = list;
}
public override string this[int position]
{
get { return itemList[position]; }
}
public override int Count {
get { return itemList.Count; }
}
public override long GetItemId(int position)
{
return position;
}

public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? inflater.Inflate(Resource.Layout.spinner_item, parent, false);

var item = itemList[position];

TextView tv = view.FindViewById<TextView>(Resource.Id.xy1z);
// here to set your Typeface
Typeface typeFace1 = Typeface.CreateFromAsset(mContext.Assets, "fonts/NotoSansCJK-Black.ttc");
tv.SetTypeface(typeFace1, TypefaceStyle.Normal);
tv.Text = itemList[position];
return view;
}
}

android spinner change font typeface

Class :

public class testActivity extends Activity {

private static final String[] COUNTRIES = new String[] { "Belgium",
"France", "Italy", "Germany", "Spain" };
private Spinner mySpinner;
private Typeface myFont;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newlay);

mySpinner = (Spinner) findViewById(R.id.spinner1);
myFont = Typeface.createFromAsset(getAssets(), "gujarti.ttf");
MyArrayAdapter ma = new MyArrayAdapter(this);
mySpinner.setAdapter(ma);
}

private class MyArrayAdapter extends BaseAdapter {

private LayoutInflater mInflater;

public MyArrayAdapter(testActivity con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return COUNTRIES.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.my_spinner_style, null);
holder = new ListContent();

holder.name = (TextView) v.findViewById(R.id.textView1);

v.setTag(holder);
} else {

holder = (ListContent) v.getTag();
}

holder.name.setTypeface(myFont);
holder.name.setText("" + COUNTRIES[position]);

return v;
}

}

static class ListContent {

TextView name;

}
}

Layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

my_spinner_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

TTF FILES

Customizing spinner font

Didn't try it, but android:typeface in XML or setTypeface() in code should work.

EDIT: Please follow the guidance here.

First, create a new XML file in your res/layout directory called "my_spinner_style.xml", and put in something like the following content:

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="9pt"
android:singleLine="True"
android:id="@+id/spinnerTarget"
android:textColor="#000000"
android:gravity="center"/>

Then in your code, use something like this:

Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
mySpinnerArrayAdapter = new MyCustomArrayAdapter(this, R.layout.my_spinner_style);
mySpinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Normally you would create a new ArrayAdapter for the second line, but in this case you need to create a custom ArrayAdapter and override the methods that get the TextView from our custom spinner style.

So, you need to put in the code for your custom ArrayAdapter, like so:

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);
return v;
}

public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(myFont);
return v;
}

}

The font you want to use needs to reside in the assets/fonts directory, and you access it like so:

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");

And that's pretty much it.

How I can set typeface (font) of this spinner text?

You can do it by-:

Just make a different layout my_layout-

<Textview
fontFamily="your font family"

</Textview>

and just change the code from-:

 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.colori, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

to-:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.colori, R.layout.my_layout);
adapter.setDropDownViewResource(R.layout.my_layout);

Setting external font for selected item of the spinner android

You can achieve this by 2 ways, first one is either take custom adapter and manage your view, and second one override your getView method for this current adapater only.

For First option of custom adapter folow below link

http://androidexample.com/Custom_Spinner_With_Image_And_Text_-_Android_Example/index.php?view=article_discription&aid=84&aaid=107

For second option check my answer below

  ArrayAdapter<CharSequence> adapterDitrict = ArrayAdapter
.createFromResource(this, R.array.district_array,
R.layout.spinner_item)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{

View v = super.getView(position, convertView, parent);
((TextView) v).setTypeFace(Your Tyope Face);
return v;
}
};

Check both answer and use any one as per your requirement.



Related Topics



Leave a reply



Submit