Android: How to change TypeFace in spinner populated by ArrayAdapter.createFromResource?

3.8k views Asked by At

I like to change my spinners typeface to "monospace".

Right now I'm populating my spinner this way:

resourceId = this.getResources().getIdentifier(country+"_umsatzsteuer_view", "array", this.getPackageName());
resourceId2 = this.getResources().getIdentifier(country+"_umsatzsteuer_werte", "array", this.getPackageName());


final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
          this, resourceId, android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
spinner_steuer.setAdapter(adapter);

My question is, how I can set the typeface of every item from this spinner to "monospace"?

UPDATED CODE:

Adaptercode:

public class YourItemAdapter extends ArrayAdapter<String> {
    public YourItemAdapter(Context _context, int _resource,
            List<String> _items) {

        super(_context, _resource, _items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //You can use the new tf here.
        View v =super.getDropDownView(position, convertView, parent);
        ((TextView) v).setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
        return v;
        }
    }

Spinnerloading:

            final ArrayAdapter<CharSequence> adapter = YourItemAdapter.createFromResource(
                      this, resourceId, android.R.layout.simple_spinner_item );
                    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

    spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
    spinner_steuer.setAdapter(adapter);

Thanks in advance! MSeiz5

2

There are 2 answers

0
Marco Seiz On BEST ANSWER

I solved the problem by myself! I have created a custom adapter and a custom spinner.

Code (JAVA) - (Mainsite.java)

String[] str_steuern;

        resourceId = this.getResources().getIdentifier(country+"_umsatzsteuer_view", "array", this.getPackageName());


        Resources steuernt = getResources();
        str_steuern = steuernt.getStringArray(resourceId);
        spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
        spinner_steuer.setAdapter(new adapter_spinner_steuer(Mainsite.this, R.layout.spinner_steuer_row, str_steuern)); 

    public class adapter_spinner_steuer extends ArrayAdapter<String>{

        public adapter_spinner_steuer(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.spinner_steuer_row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.steuer);
            label.setText(str_steuern[position]);
            return row;
            }
        }

XML: (spinner_steuer_row.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>

    <TextView
        android:id="@+id/steuer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:typeface="monospace" />

</RelativeLayout>
2
Hamad On

I suggest you to implement your Adapter in another file,then you can change type face and also can access the "getAssets()" function from the constructor of the Adapter, as you have the Context as a parameter.

public class YourItemAdapter extends ArrayAdapter<String> {
int recurso;
Typeface tf;

public YourItemAdapter(Context _context, int _resource,
        List<String> _items) {

    super(_context, _resource, _items);
    recurso=_resource;
    tf=Typeface.createFromAsset(_context.getAssets(),"font/digital-7.ttf");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //You can use the new tf here.
    TextView spinner_text=(TextView)findViewById(R.id.text1);
    spinner_text.setTypeface(tf);
    }
}