2
votes

my problem is that how can change font in hindi in custom listview in android.I am using this code Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/DroidHindi.ttf"); // Adding menuItems to ListView

        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost });
        // Typeface tf = Typeface.createFromAsset(getAssets(),   "fonts/DroidHindi.ttf");

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

please help me out. thanks in advance!!!!!!!!!

i am override the getView method but can not work.please help me.

2

2 Answers

1
votes

Override getView method of the SimpleAdapter class, and Set Font on textview, which you want to change font, as below:

ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost }){

 public View getView(View view)
{
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
    Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/DroidHindi.ttf"); 
    LinearLayout llMain=(LinearLayout)inflater.inflate(R.layout.list_item, null);
    TextView txtName=(TextView)llMain.findViewById(R.id.name);
    txtName.setTypeface(hindiFont); 
    TextView txtDescription=(TextView)llMain.findViewById(R.id.desciption);
    txtDescription.setTypeface(hindiFont);
    return llMain;
}
};
2
votes

put hindi truetype font file in asset folder

and create custom adapter

public class MyArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_mobile, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_mobile, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.label);

        //here use your true type font of hindi like this hindiFonts.ttf is placed inside asset/fonts/ folder of project
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/hindiFonts.ttf"); 
        textView .setTypeface(face); 
        textView.setText(values[position]);


        return rowView;
    }
}

set above custom adapter in your listview

String[] MOBILE_OS = 
               new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};


        myListView.setListAdapter(new MyArrayAdapter(this, MOBILE_OS));