2

I'm not able to put the custom font through Typeface, as getAssets() is not showing up while writing createFromAsset. I've used getContext(), getActivity(), placed the assets in the project and not in src, all not finding soln. Please tell me the error.

package com.example.shubhojit.careersafter10th.ViewHolder; import android.content.res.AssetManager; import android.graphics.Typeface; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.example.shubhojit.careersafter10th.Interface.ItemClickListener; import com.example.shubhojit.careersafter10th.R; public class Courses_After10thViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView txtCourseName; public ImageView courseImage; public TextView txtCourseDuration; Typeface courseName; Typeface courseDuration; private ItemClickListener itemClickListener; public Courses_After10thViewHolder(View itemView) { super(itemView); txtCourseName = (TextView)itemView.findViewById(R.id.courses_after10th_name); courseImage = (ImageView)itemView.findViewById(R.id.courses_after10th_image); txtCourseDuration = (TextView)itemView.findViewById(R.id.courses_after10th_duration); courseName = Typeface.createFromAsset(context.getAssets(),"Fonts/Antipasto-RegularTrial.ttf"); itemView.setOnClickListener(this); } public void setItemClickListener(ItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } @Override public void onClick(View view) { itemClickListener.onClick(view,getAdapterPosition(),false); } } 
2
  • I can't see what is the context in your code. Do you pass the context to your class correctly? Commented Oct 8, 2018 at 2:11
  • @RickyMo there's no need to pass the context, one probably cannot even pass it into there. Commented Oct 8, 2018 at 2:26

2 Answers 2

1

You have to crate assets under Fonts folder then place Antipasto-RegularTrial.ttf:

courseName = Typeface.createFromAsset(context.getAssets(),"Fonts/Antipasto-RegularTrial.ttf"); txtCourseName.setTypeface(courseName); 
Sign up to request clarification or add additional context in comments.

1 Comment

this does not solve the problem, that the context is unknown.
0

within the scope of a RecyclerView.ViewHolder, you can obtain a handle to the Context alike shown in below code (it also considers View.isInEditMode(), to make the XML preview work):

/* obtain a handle to the parent RecyclerView */ this.mRecyclerView = (SomeRecyclerView) viewHolder.getParent(); /* obtain a handle to the it's Context */ Context context; if(viewHolder.isInEditMode()) {context = ((ContextThemeWrapper) this.mRecyclerView.getContext()).getBaseContext();} else {context = this.mRecyclerView.getContext();} 

most likely, Fonts/Antipasto-RegularTrial.ttf should be renamed to fonts/antipasto_regulartrial.ttf, in order to be a valid resource descriptor. this may vary in between assets and resources; see Fonts in XML - this explains how this can be done meanwhile.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.