Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Gujarati text on Spinner in Android

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);

}
like image 565
apatel Avatar asked Jan 30 '13 10:01

apatel


1 Answers

You have to look in to this:

enter image description here

check below code:

package com.example.main.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.testapp.R;

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;

    }
}

Best Luck!

like image 152
Dhaval Parmar Avatar answered Oct 01 '22 19:10

Dhaval Parmar