Skip to content Skip to sidebar Skip to footer

Android Change Textview Textsize On Recyclerview Adapter From Activity

i'm trying to find how can i change my RecyclerView adapter textViews from Activity, in my activity i have two widgets such as increment_text_size and decrement_text_size which the

Solution 1:

You do not have to create a listener for this purpose. You should hold a field named textSize in your adapter. Then, set this whenever you want from your activity.

publicclassShowBookContentsAdapterextendsRecyclerView.Adapter<ShowBookContentsAdapter.ShowBookContentsViewHolder> {

    privateint textSize;

    // constructor etc.@Overridepublic ShowBookContentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        finalViewview= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book_content_paragraph, parent, false);
        final ShowBookContentsViewHolder holder newShowBookContentsViewHolder(view);

        return holder;
    }

    @OverridepublicvoidonBindViewHolder(ShowBookContentsViewHolder holder, finalint position) {
        implementingHeadingParagraphView(holder, position);
    }

    privatevoidimplementingHeadingParagraphView(final ShowBookContentsViewHolder holder, finalint position) {
        Utils.overrideFonts(context, holder.book_content_paragraph, PersianFontType.SHABNAM);

        holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

        holder.book_content_paragraph.setText(Html.fromHtml(list.get(position).getContent()));

    }

    publicvoidsetTextSizes(int textSize) {
        this.textSize = textSize;
        notifyDataSetChanged();
    }

    //... other adapter methodspublicclassShowBookContentsViewHolderextendsRecyclerView.ViewHolder {
        @Nullable@BindView(R.id.book_content_paragraph)
        TextView book_content_paragraph;

        @Nullable@BindView(R.id.book_content_heading_one)
        TextView book_content_heading_one;

        publicShowBookContentsViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

call this from your activity

showBookContentsAdapter.setTextSizes(18);

Solution 2:

You have to call notifydatasetchanged from you activity

1.First, save the font size on constant variable if temporary or use shared preferences if need in whole life cycle of app

  1. Make a method in your activity to save font size

    privatevoidsaveFontSize(boolean isFont){
     IS_LARGE_FONT= isFont;
     recyclerView.post(newRunnable(){
     adapter.notifyDataSetChanged();
     });
    }
    
  2. In your adapter class just check that value in bindholder

    if(IS_LARGE_FONT)
    {
    //set large font
    }
    else{
    // set small font
    }
    

Post a Comment for "Android Change Textview Textsize On Recyclerview Adapter From Activity"