2

I am using RecyclerView to show three different text and I want to apply a custom Typeface to each one. Now in my adapter under ViewHolder I have put the Typeface code but not sure how to apply them to each textView.

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

List<AdapterData> mItems;

public Adapter() {
    super();
    mItems = new ArrayList<>();
    AdapterData data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");
    mItems.add(data);

    data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");

    data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");
    mItems.add(data);


}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.cardview_items, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    AdapterData data = mItems.get(i);
    viewHolder.textOne.setText(data.getTextOne());
    viewHolder.textTwo.setText(data.getTextTwo());
    viewHolder.textThree.setText(data.getTextThree());

}


@Override
public int getItemCount() {

    return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView textOne;
    public TextView textTwo;
    public TextView textThree;

    Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
    Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
    Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");


    public ViewHolder(View itemView) {
        super(itemView);
        textOne = (TextView)itemView.findViewById(R.id.textView1);
        textTwo = (TextView)itemView.findViewById(R.id.textView2);
        textThree = (TextView)itemView.findViewById(R.id.textView3);


    }
}
}
Jacques Krause
  • 5,523
  • 9
  • 27
  • 43
  • Possible duplicate of [Using a custom typeface in Android](http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android) – Evgeniy Mishustin Jul 19 '16 at 09:50

5 Answers5

2
class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textOne;
        public TextView textTwo;
        public TextView textThree;

        Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
        Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
        Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");


        public ViewHolder(View itemView) {
            super(itemView);
            textOne = (TextView) itemView.findViewById(R.id.textView1);
            textOne.setTypeface(customTypeOne);
            textTwo = (TextView) itemView.findViewById(R.id.textView2);
            textTwo.setTypeface(customTypeTwo);
            textThree = (TextView) itemView.findViewById(R.id.textView3);
            textThree.setTypeface(customTypeThree);
        }
    }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

You actually just have to add the typefaces to the textview:

public ViewHolder(View itemView) {
    super(itemView);
...
textOne.setTypeface(customTypeOne)
...
miqueloi
  • 688
  • 4
  • 13
0

You can use setTypeface() method to set the created typeface on yout textview:

textOne.setTypeface(customTypeOne);
textTwo.setTypeface(customTypeTwo);
textThree.setTypeface(customTypeThree);

I will also suggest you look into Calligraphy on github. It is a powerful library to handle typeface dilemma efficiently :)

https://github.com/chrisjenx/Calligraphy

Kushan
  • 5,855
  • 3
  • 31
  • 45
0
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
viewHolder.textOne.setTypeface(myTypeface);
viewHolder.textOne.setText(data.getTextOne());
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
0
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

List<AdapterData> mItems;

public Adapter() {
    super();
    mItems = new ArrayList<>();
    AdapterData data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");
    mItems.add(data);

    data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");

    data = new AdapterData();
    data.setTextOne("Title 1");
    data.setTextTwo("Title 2");
    data.setTextThree("Title 3");
    mItems.add(data);


}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.cardview_items, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    AdapterData data = mItems.get(i);
    viewHolder.textOne.setText(data.getTextOne());
    viewHolder.textTwo.setText(data.getTextTwo());
    viewHolder.textThree.setText(data.getTextThree());

}


@Override
public int getItemCount() {

    return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView textOne;
    public TextView textTwo;
    public TextView textThree;

    Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
    Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
    Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");


    public ViewHolder(View itemView) {
        super(itemView);
        textOne = (TextView)itemView.findViewById(R.id.textView1);
        textTwo = (TextView)itemView.findViewById(R.id.textView2);
        textThree = (TextView)itemView.findViewById(R.id.textView3);


        textOne.setTypeface(customTypeOne);
        textTwo.setTypeface(customTypeTwo);
        textThree.setTypeface(customTypeThree);


    }
}
}
Jenis Kasundra
  • 583
  • 9
  • 19