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.

public class ShowBookContentsAdapter extends RecyclerView.Adapter<ShowBookContentsAdapter.ShowBookContentsViewHolder> {

    private int textSize;

    // constructor etc.

    @Override
    public ShowBookContentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

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

        return holder;
    }

    @Override
    public void onBindViewHolder(ShowBookContentsViewHolder holder, final int position) {
        implementingHeadingParagraphView(holder, position);
    }

    private void implementingHeadingParagraphView(final ShowBookContentsViewHolder holder, final int 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()));

    }

    public void setTextSizes(int textSize) {
        this.textSize = textSize;
        notifyDataSetChanged();
    }

    //... other adapter methods

    public class ShowBookContentsViewHolder extends RecyclerView.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;

        public ShowBookContentsViewHolder(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

    private void saveFontSize(boolean isFont){
     IS_LARGE_FONT= isFont;
     recyclerView.post(new Runnable(){
     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"