android - How to set Custom Font for a TextView in RecyclerView? -


i using recyclerview populate data cloud. looks like, setting custom font textview not easy inside view. traditional way, tried :

textview tx = (textview)findviewbyid(r.id.person_age);      typeface custom_font = typeface.createfromasset(getassets(), "fonts/dancing script.ttf");      tx.settypeface(custom_font); 

but here , crashes app, looking fix, here code main activiy:

    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     parse.initialize(this, "app-id", "clientkey");     setcontentview(r.layout.activity_big_board);     initializedata();     textview tx = (textview)findviewbyid(r.id.person_age);      typeface custom_font = typeface.createfromasset(getassets(), "fonts/dancing script.ttf");      tx.settypeface(custom_font);     adapter = new rvadapter(persons);       rv=(recyclerview)findviewbyid(r.id.rv);      linearlayoutmanager llm = new linearlayoutmanager(this);     rv.setlayoutmanager(llm);     rv.sethasfixedsize(true);       initializeadapter();  }  private void initializedata(){     persons = new arraylist<>();       parsequery<parseobject> query = new parsequery<parseobject>("bigboard");     query.findinbackground(new findcallback<parseobject>() {         public void done(list<parseobject> credentiallist, parseexception e) {             if (e == null) {                 for(int i=0;i<credentiallist.size();i++)                 {                     a=credentiallist.get(i).getstring("location");                     b=credentiallist.get(i).getstring("feed");                     persons.add(new person(a,b));                        log.d("out", "so val::------> " +a +b);                   }             } else {                 log.d("score", "error: " + e.getmessage());             }              adapter.notifydatasetchanged();         }     });   }  private void initializeadapter(){      rv.setadapter(adapter); } 

and here code adapter class:

public class rvadapter extends recyclerview.adapter<rvadapter.personviewholder> {  public static class personviewholder extends recyclerview.viewholder {      cardview cv;     textview personname;     textview personage;     //imageview personphoto;      personviewholder(view itemview) {         super(itemview);         cv = (cardview)itemview.findviewbyid(r.id.cv);         personname = (textview)itemview.findviewbyid(r.id.person_name);         personage = (textview)itemview.findviewbyid(r.id.person_age);         //personphoto = (imageview)itemview.findviewbyid(r.id.person_photo);     } }  list<person> persons;  rvadapter(list<person> persons){     this.persons = persons; }  @override public void onattachedtorecyclerview(recyclerview recyclerview) {     super.onattachedtorecyclerview(recyclerview); }  @override public personviewholder oncreateviewholder(viewgroup viewgroup, int i) {     view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.item, viewgroup, false);     personviewholder pvh = new personviewholder(v);     return pvh; }  @override public void onbindviewholder(personviewholder personviewholder, int i) {     personviewholder.personname.settext(persons.get(i).name);     personviewholder.personage.settext(persons.get(i).surname);    // personviewholder.personphoto.setimageresource(persons.get(i).photoid); }  @override public int getitemcount() {     if(persons!=null)     {         return persons.size();     }     else     {         return 0;     } } 

}

so, what's best way without causing problems ui thread? thanks,

move typeface logic personviewholder, in constructor, 2 reasons:

  1. the textview exists point.

  2. your recyclerview have more 1 personviewholder, in case need set typeface in every personviewholder, because there more 1 item , more 1 textview needs typeface.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -