0

can i do it in MainActivity.java without creating another .java file? here is my code:

lv=(ListView) findViewById(R.id.listView1);
strArr=new ArrayList<String>();

for(int i=0; i<2 ;i++){ 
    strArr.add("Row"+i);            
    }

Typeface cFont = Typeface.createFromAsset(getAssets(), "cvv.ttf");
    lv.setTypeface(cFont);
  adapter=new ArrayAdapter<String>(getApplicationContext(),  android.R.layout.simple_list_item_1,strArr);
    lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
  • Already answerer here [Custom font for Android listview][1] [1]: http://stackoverflow.com/questions/15293437/custom-font-for-android-listview – Jakub Szymion Aug 20 '14 at 08:40

2 Answers2

1

You will need to override the getView method of your adapter. Inside of it you can modify the font for every ListView's item.

Something like:

final Typeface cFont = Typeface.createFromAsset(getAssets(), "cvv.ttf");
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, strArr) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);
        if(convertView == null) {
            textView.setTypeFace(cFont);
        }
        return textView;
    }
};
Simas
  • 43,548
  • 10
  • 88
  • 116
1

It's possible if you override the arrayadapter in the same page. This way you'll be able to access the listview itemtemplate and change it's typeface.

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, strArr) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);
        if(convertView == null) {
            convertView = layoutInflater.inflate( android.R.layout.simple_list_item_1, parent);

convertView.findViewById<TextView>(Resource.Id.text1).setTypeFace(cFont);
        }
        return view;
    }
};
Chris
  • 3,329
  • 1
  • 30
  • 44