There is a RecyclerView, it has CardView, which contains a TextView. Everything is filled in the adapter. Everything is working.

In the activation, I get the font size from the dialog and I want, what after that the TextView in CardView would be displayed by this font size. But everything is displayed in the size that was specified during creation.

How to implement this change the font size during the execution of the application?


In PostAdapter:

private int mFontSize; ... public void setFontSize(int fontSize) { mFontSize = fontSize; } public ViewHolder onCreateViewHolder{ View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post, parent, false); TextView tvpost = (TextView) v.findViewById(R.id.postitem_post); tvpost.setTextSize(TypedValue.COMPLEX_UNIT_SP, mFontSize); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, tvpost.getLineHeight() * LINE_COUNT_FOR_SHORT_VIEW); tvpost.setLayoutParams(lp); return new ViewHolder(v); } @Override public void onBindViewHolder(final ViewHolder holder, int position) { PostModel post = posts.get(position); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { holder.post.setText(Html.fromHtml(post.getElementPureHtml(), Html.FROM_HTML_MODE_LEGACY)); } else { holder.post.setText(Html.fromHtml(post.getElementPureHtml())); } holder.post.setTextSize(TypedValue.COMPLEX_UNIT_SP, mFontSize); ... 

In MainActivity:

 @Override protected void onCreate(Bundle savedInstanceState) { ... PostAdapter adapter = new PostAdapter(this, mPosts, mFontSize); mRvPosts.setAdapter(adapter); ... } //Сюда возвращаемся после выхода из настроек @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // читаем размер шрифта из ListPreference mFontSize = Integer.parseInt(prefs.getString(getString(R.string.pref_font_size), "18")); PostAdapter pa = (PostAdapter) mRvPosts.getAdapter(); pa.setFontSize(mFontSize); mRvPosts.getAdapter().notifyDataSetChanged(); super.onActivityResult(requestCode, resultCode, data); } 

When the application is restarted, it works, the font size changes, and when it is changed in the settings and in the running application ...

  • one
    The code in the studio ... - Barmaley

1 answer 1

Approximate procedure such

  1. Write font size in SharedPreferences
  2. In onBindViewHolder assign a font size to a text field using the previously written value from SharedPreferences
  3. Somewhere (activit / snippet) register a SharedPreferences change SharedPreferences . In it, call the adapter redraw by calling notifyDataSetChanged()
  • It doesn't work like that ... - xsid
  • @xsid "does not work" - a little informative. There is always a reason for “not working”. For example, the error shown in the logs, or a compilation error. Maybe even just the method is not called. Or caused not there, not then or not. In any case - why then mark the answer "true"? - Yuriy SPb
  • one
    Thanks for the answer. I don’t know what changed where, but when I started reading the font size value directly from the SharedPreferences settings (item 2), it all worked. Therefore, marked the answer as resolved. Before that I read the font size from the settings 1 time, and tried to set the size of this variable. - xsid