Setting a typeface to text in listview in android

498 views Asked by At

I want to set the typeface of the list view into araliya.ttf. Here is my code. There is no error but typeface in listview text is not changed.(It is changed in textviews and buttons) Can someone please tell me what is wrong in here. Thankyou..

public class MyCustomAdapter extends ArrayAdapter<Item> {

        private ArrayList<Item> itemList;
        Typeface tf;

        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Item> itemList) {
            super(context, textViewResourceId, itemList);
            this.itemList = new ArrayList<Item>();
            this.itemList.addAll(itemList);
           // tf = Typeface.createFromAsset(context.getAssets(), "araliya.ttf");
        }

        private class ViewHolder {
            TextView code;
            CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.symptom_info, null);
                sinfont = Typeface.createFromAsset(getAssets(), "araliya.ttf");
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.code.setTypeface(sinfont);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.name.setTypeface(sinfont);
                convertView.setTag(holder);

Here is my code in oncreate()

dataAdapter = new MyCustomAdapter(this,
                R.layout.symptom_info, itemList);
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(dataAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Item item = (Item) parent.getItemAtPosition(position);

            }
        });

UPDATE - ISSUE RESOLVED

TextView t1 = (TextView)findViewById(R.id.textView2); 
t1.setTypeface(sinfont);
t1.setText("my,ska m%fNoh f;dard.kak"); 

This works perfectly and change the typeface of the text in textview.

1

There are 1 answers

3
capt.swag On

I am pretty sure that, you haven't set the path right.

Typeface sinfont = Typeface.createFromAsset(getAssets(), "araliya.ttf");

Here araliya.ttf is not the correct path. It should be fonts/araliya.ttf.

Actually the seconds argument inside createFromAsset stands for path to the font. So if you have put the font in assets/fonts. Then below is the required code to initialise the font.

String path = "fonts/araliya.ttf"
Typeface sinfont = Typeface.createFromAsset(getAssets(), path);

Then you could set font using

holder.code.setTypeface(sinfont);