Chaminda Nuwan
Chaminda Nuwan

Reputation: 35

i cant change ListView font, but edittext and textview custom fonts are working

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

Upvotes: 0

Views: 101

Answers (2)

Chris
Chris

Reputation: 3338

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

Upvotes: 1

Simas
Simas

Reputation: 44148

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

Upvotes: 1

Related Questions