4

I would like to change the font of my 'item' in recycleview with custom font. Anybody can help me with this? I manage to change the font for my header by using

header_title = (TextView) itemView.findViewById(R.id.header_title); 

and

Typeface face_01 = Typeface.createFromAsset(itemView.getContext().getAssets(), "customfont/grb.otf");
 header_title.setTypeface(face_01);

But i have no idea how to do that for item in arraylist.

Here is the java code for my adapter:-

    public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        public static final int HEADER = 0;
        public static final int CHILD = 1;

        private List<Item> data;

        public ExpandableListAdapter(List<Item> data) {
            this.data = data;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
            View view = null;
            Context context = parent.getContext();
            float dp = context.getResources().getDisplayMetrics().density;
            int subItemPaddingLeft = (int) (18 * dp);
            int subItemPaddingTopAndBottom = (int) (5 * dp);
            switch (type) {
                case HEADER:
                    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.list_header, parent, false);
                    ListHeaderViewHolder header = new ListHeaderViewHolder(view);
                    return header;
                case CHILD:
                    TextView itemTextView = new TextView(context);
                    itemTextView.setPadding(subItemPaddingLeft, subItemPaddingTopAndBottom, 0, subItemPaddingTopAndBottom);
                    itemTextView.setTextColor(0x88000000);
                    itemTextView.setLayoutParams(
                            new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
                    return new RecyclerView.ViewHolder(itemTextView) {
                    };
            }
            return null;
        }

        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final Item item = data.get(position);
            switch (item.type) {
                case HEADER:
                    final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
                    itemController.refferalItem = item;
                    itemController.header_title.setText(item.text);
                    if (item.invisibleChildren == null) {
                        itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                    } else {
                        itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                    }
                    itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (item.invisibleChildren == null) {
                                item.invisibleChildren = new ArrayList<Item>();
                                int count = 0;
                                int pos = data.indexOf(itemController.refferalItem);
                                while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
                                    item.invisibleChildren.add(data.remove(pos + 1));
                                    count++;
                                }
                                notifyItemRangeRemoved(pos + 1, count);
                                itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                            } else {
                                int pos = data.indexOf(itemController.refferalItem);
                                int index = pos + 1;
                                for (Item i : item.invisibleChildren) {
                                    data.add(index, i);
                                    index++;
                                }
                                notifyItemRangeInserted(pos + 1, index - pos - 1);
                                itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                                item.invisibleChildren = null;
                            }
                        }
                    });
                    break;
                case CHILD:
                    TextView itemTextView = (TextView) holder.itemView;
                    itemTextView.setText(data.get(position).text);
                    break;
            }
        }

        @Override
        public int getItemViewType(int position) {
            return data.get(position).type;
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
            public TextView header_title;
            public ImageView btn_expand_toggle;
            public Item refferalItem;

            public ListHeaderViewHolder(View itemView) {
                super(itemView);
                header_title = (TextView) itemView.findViewById(R.id.header_title);
                btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);

                Typeface face_01 = Typeface.createFromAsset(itemView.getContext().getAssets(), "customfont/grb.otf");
                header_title.setTypeface(face_01);
            }
        }

        public static class Item {
            public int type;
            public String text;
            public List<Item> invisibleChildren;

            public Item() {
            }

            public Item(int type, String text) {
                this.type = type;
                this.text = text;
            }
        }
    }
ugur
  • 3,604
  • 3
  • 26
  • 57
Tan Kahseng
  • 71
  • 1
  • 9

1 Answers1

3

Gloabally define fontpath and typeface

 Typeface typeface;

String fontpath = "fonts/KaushanScript-Regular.otf";

In your constructor do following

public ExpandableListAdapter(Context context,List<Item> data) {
typeface = Typeface.createFromAsset(context.getAssets(), fontpath);
            this.data = data;
        }

In your BindViewHolder

holder.itemTextView.setTypeface(typeface);
Shashwat Gupta
  • 876
  • 9
  • 22
  • Thanks @Shashwat Gupta, but i still have 1 error at mainactivity. For this part: recyclerview.setAdapter(new ExpandableListAdapter(data)); Error:(58, 33) error: constructor ExpandableListAdapter in class ExpandableListAdapter cannot be applied to given types; required: Context,List found: List reason: actual and formal argument lists differ in length – Tan Kahseng Sep 11 '17 at 09:03
  • from your activity where you call the constructor of your adapter pass the context of your activity as 1st paramaters – Shashwat Gupta Sep 11 '17 at 09:04
  • this is how i pass context, what you mean by first parameter? ExpandableListAdapter.Item places = new ExpandableListAdapter.Item(ExpandableListAdapter.HEADER, "Data1"); places.invisibleChildren = new ArrayList<>(); places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, "sub Data1")); places.invisibleChildren.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, " sub Data1")); data.add(places); recyclerview.setAdapter(new ExpandableListAdapter(data)); – Tan Kahseng Sep 11 '17 at 09:11
  • ExpandableListAdapter.Item places = new ExpandableListAdapter.Item(YourActivity.this, "Data1"); – Shashwat Gupta Sep 11 '17 at 10:22
  • as in your constructor of adapter you are gettign public ExpandableListAdapter(Context context,List data){ } So you need to pass activity instance to this context – Shashwat Gupta Sep 11 '17 at 10:23