How to set custom font for android listview?

好久不见. 提交于 2019-11-29 10:28:31

Try this...

You can override the getView() when creating your Adapter in Activity, for example of a SimpleAdapter with name and title.

//global declaration
ListAdapter adapter;

adapter = SimpleAdapter(getActivity(), your_list, R.layout.your_list_items, new String[]{"Name", "Title"}, new int[]{R.id.name, R.id.title}){ 


@Override
 public View getView(int position,  View convertView, ViewGroup parent) {
           v = convertView;
     if(v == null){
     LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     v = li.inflate(R.layout.interview_list_items, null);
     TextView titleText= (TextView) v.findViewById(R.id.title);
     TextView nameText= (TextView) v.findViewById(R.id.name);
     childFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Light.ttf");
     titleText.setTypeface(childFont);
    nameText.setTypeface(childFont);
    return super.getView(position, v, parent);  
    }                                   
};                      

Or try it in Adapter class like this...

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

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_home, values);
    this.context = context;
    this.values = values;
    this.tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); //initialize typeface here.
}

@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_home, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);

    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setTypeface(tf); // set typeface here
    textView.setText(values[position]);


    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("QuickStart")) {
        imageView.setImageResource(R.drawable.quick_strat);
    } else if (s.equals("Custom")) {
        imageView.setImageResource(R.drawable.customize);
    } else if (s.equals("CallList")) {
        imageView.setImageResource(R.drawable.call_list);
    } else if(s.equals("Calendar")){
        imageView.setImageResource(R.drawable.calendar);
    } else if(s.equals("Templates")){
        imageView.setImageResource(R.drawable.templates);
    }
    else{
        imageView.setImageResource(R.drawable.user_guide);
    }

    return rowView;
}

}

app directory structure

use this code

In Your MobileArrayAdapter

class MobileArrayAdapter extends ArrayAdapter<CharSequence>{
            Typeface tf; 
        public MobileArrayAdapter(Context context, String[] values,String FONT) {
                tf = Typeface.createFromAsset(context.getAssets(), FONT);
            }

From Where You Are calling your adapter

listAdapter = new MobileArrayAdapter(this, R.layout.custom_list_text, R.array.arrayName, "name_of_font.ttf");

you just need to change the font in this layout "R.layout.list_home". Because list_home is layout which is working as a single row. So if you changes it, it will affects the color/text/style of listview too.

Put a custom font file .ttf in assets folder and use the below code to change font

text.setTypeface(Typeface.createFromAsset(getAssets(), "/*path to your font file */"));

Path would be the path in assets. If file is in assets folder only the use only the file name with extension.

Like

 text.setTypeface(Typeface.createFromAsset(getAssets(), "arial.ttf"));

Use

setTypeface(Typeface)

in Textview object.

Rajkumar prajapati

This is my custom adapter how to call and which parameters are give in my call activity.

public class CustomData extends ArrayAdapter<CharSequence>    
{
   Context context;
   int layoutResourceId;
   CharSequence data[] = null;
   Typeface tf;

   public CustomData(Context context, int layoutResourceId, CharSequence[] data, String FONT) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
       tf = Typeface.createFromAsset(context.getAssets(), FONT);
   }
}

I use this code in my Adapter class and it work:

public class MyAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Item> item;

    public MyAdapter(Context context, ArrayList<Item> item) {
        this.context = context;
        this.item = item;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public Object getItem(int i) {
        return item.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_header, viewGroup, false);
        }

        Typeface MyFont= Typeface.createFromAsset(context.getAssets(), "Arial.ttf");
        TextView txtTitle = view.findViewById(R.id.txtTitle);
        txtTitle.setTypeface(MyFont);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!