I am using a RecyclerView
to populate the data from cloud. But it looks like, setting custom font for a TextView
is not easy as its inside the view. Traditional way, this is what I 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 , it crashes the app, So looking for fix, here is the code to the 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 the Val::------> " +a +b);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
adapter.notifyDataSetChanged();
}
});
}
private void initializeAdapter(){
rv.setAdapter(adapter);
}
And here is the code to the 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 the best way to do this without causing any problems to the UI Thread? Thanks,