I am trying to use font awesome in my app. But i dont know how to use it without passing context as a parameter. This is my xml file:
<Button
android:layout_below="@id/author"
android:id="@+id/compare_btn"
android:layout_toRightOf="@id/cart_btn"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="@string/fa_random" />
and this is my Holder class which ofcourse does not extend activity nor fragment.
public class ListRowViewHolder extends RecyclerView.ViewHolder {
protected NetworkImageView productImageURL;
protected TextView review;
protected TextView productName;
protected RelativeLayout recLayout;
protected TextView categoryId;
protected TextView price;
protected Button wishListBtn, cartBtn, compareBtn;
public ListRowViewHolder(View view) {
super(view);
this.productImageURL = (NetworkImageView) view.findViewById(R.id.thumbnail);
this.productName = (TextView) view.findViewById(R.id.title);
this.review = (TextView) view.findViewById(R.id.url);
this.recLayout = (RelativeLayout) view.findViewById(R.id.recLayout);
this.categoryId = (TextView) view.findViewById(R.id.subreddit);
this.price = (TextView) view.findViewById(R.id.author);
this.wishListBtn=(Button) view.findViewById(R.id.wishlist_btn);
this.cartBtn=(Button) view.findViewById(R.id.cart_btn);//set Font Awesome here
this.compareBtn=(Button) view.findViewById(R.id.customer_button);//
view.setClickable(false);
}
}
and this is a method in my recycler adapter that set's the text of textview based on json data
@Override
public void onBindViewHolder(final ListRowViewHolder listRowViewHolder, int position) {
ListItems listItems = listItemsList.get(position);
listRowViewHolder.itemView.setSelected(focusedItem == position);
String local ="http://carrottech.com/lcart/media/catalog/product/"+listItems.getProductImageURL().trim();
System.out.print(local+"sdaaaaaaaaaaaaaaaaaaaaaa");
listRowViewHolder.getLayoutPosition();
mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
listRowViewHolder.productImageURL.setImageUrl(local, mImageLoader);
listRowViewHolder.productImageURL.setDefaultImageResId(R.drawable.reddit_placeholder);
listRowViewHolder.productName.setText(Html.fromHtml(listItems.getProductName()));
listRowViewHolder.categoryId.setText(Html.fromHtml(String.valueOf(listItems.getCategoryId())));
listRowViewHolder.price.setText(Html.fromHtml(String.valueOf(listItems.getProductPrice())));
listRowViewHolder.review.setText(Html.fromHtml("Write Review"));
listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));
}
I have been setting Font awesome using a typeface class like this:
public class FontManager {
public static final String ROOT = "fonts/",
FONTAWESOME = "fonts/fontawesome-webfont.ttf";
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
}
and in my activity or fragment i used to
Typeface iconCalendar = FontManager.getTypeface(getContext(), FontManager.FONTAWESOME);
jcustomerDobIcon.setTypeface(iconCalendar);
but how to do it if dont have a context to pass to the FontManager class? Please help!
how to do it if dont have a context to pass to the FontManager class
Get Context
from View's which is in ListRowViewHolder
class using View.getContext()
method.
Like in onBindViewHolder
method pass Context to FontManager
class as:
Typeface iconCalendar = FontManager.getTypeface(
listRowViewHolder.wishListBtn.getContext(),
FontManager.FONTAWESOME);
listRowViewHolder.wishListBtn.setTypeface(iconCalendar);
listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));
Try this,
Typeface m_font = FontManager.getTypeface(
listRowViewHolder.productName.getContext(),
FontManager.FONTAWESOME);
listRowViewHolder.wishListBtn.setTypeface(m_font);
you can set for as per your need.
I had a context in the constructor which i didnt see so this is how i did it. Hope it helps someone in the future
public MyRecyclerAdapter(Context context, List<ListItems> listItemsList) {
this.listItemsList = listItemsList;
this.mContext = context;
}
@Override
public ListRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
ListRowViewHolder holder = new ListRowViewHolder(v);
holder.recLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("List Size", Integer.toString(getItemCount()));
TextView redditUrl = (TextView) v.findViewById(R.id.url);
String postUrl = redditUrl.getText().toString();
Log.d("THe URS", postUrl);
Intent intent = new Intent(mContext, WebViewActivity.class);
intent.putExtra("url", postUrl);
mContext.startActivity(intent);
}
});
return holder;
}
@Override
public void onBindViewHolder(final ListRowViewHolder listRowViewHolder, int position) {
Typeface icon = FontManager.getTypeface(mContext, FontManager.FONTAWESOME);
ListItems listItems = listItemsList.get(position);
listRowViewHolder.itemView.setSelected(focusedItem == position);
String local ="http://carrottech.com/lcart/media/catalog/product/"+listItems.getProductImageURL().trim();
System.out.print(local+"sdaaaaaaaaaaaaaaaaaaaaaa");
listRowViewHolder.getLayoutPosition();
mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
listRowViewHolder.productImageURL.setImageUrl(local, mImageLoader);
listRowViewHolder.productImageURL.setDefaultImageResId(R.drawable.reddit_placeholder);
listRowViewHolder.productName.setText(Html.fromHtml(listItems.getProductName()));
listRowViewHolder.categoryId.setText(Html.fromHtml(String.valueOf(listItems.getCategoryId())));
listRowViewHolder.price.setText(Html.fromHtml(String.valueOf(listItems.getProductPrice())));
listRowViewHolder.review.setText(Html.fromHtml("Write Review"));
listRowViewHolder.wishListBtn.setTypeface(icon);
listRowViewHolder.cartBtn.setTypeface(icon);
listRowViewHolder.compareBtn.setTypeface(icon);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.