1

To begin I've got an app with 2 activities. 1st is the main one. On the 2nd there are 3 checkboxes that are changing the typeface of textview in main activity (Bold, Italic and Underline). What I want to do is after closing and reopening app the text view has the typeface that was previously selected. Is there any way to store Typeface in shared preferences, or it can be done in some other way?

Thanks

UPDATE

This works (third approach from below answer)

    boolean loadCheck1 = preferences.getBoolean("checked1", false);
    if (loadCheck1) {
            tx.setTypeface(tx.getTypeface(), Typeface.BOLD);
    } else {
        tx.setTypeface(null, Typeface.NORMAL);
    }

    boolean loadCheck2 = preferences.getBoolean("checked2", false);
    if (loadCheck2) {
        tx.setTypeface(tx.getTypeface(), Typeface.ITALIC);
    } else {
        tx.setTypeface(null, Typeface.NORMAL);
    }

    boolean loadCheck3 = preferences.getBoolean("checked3", false);
    if (loadCheck3){
        tx.setPaintFlags(tx.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
    } else {
    tx.setPaintFlags(tx.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG));
    }

But there is a problem with Typeface Bold. It even if the checkbox1 is checked Bold is not appearing. It is really strange, cause every single If is made in the same way. In addition if I'll change the order (for example loadcheck2, loadcheck3, loadcheck1) then Italic is not appearing.

Karol
  • 27
  • 6

1 Answers1

1
  • You can use GSON library to store Object class (Typeface extends Object) in SharedPrefereces. More information about this approach: How Android SharedPreferences save/store object

  • If you do not want to use any external libraries, you could make an enum or interface class with variables that would represent specific Typeface that you want to save and store that variable in SharedPreferences. When user reopens the app just retrieve that variable and use switch statement to determine which Typeface to use.

  • Store a value in SharedPreferences that represents which CheckBox is selected before an Actvity is destroyed. Then on start of Activity retrieve that value and check which CheckBox was selected, then choose corresponding Typeface.


I would recommend using second or third approach, because storing Typeface in SharedPreferences might bring some performance drawbacks compared to other approaches.

Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
  • Great! Thanks a lot! But I've another problem. Pleas see updated question. – Karol Apr 05 '18 at 12:36
  • I am glad it worked. I would suggest you to add some breakpoints and debug your code. – Tomas Jablonskis Apr 05 '18 at 12:44
  • By the way, your goal is to use few typefaces on one another like: Bold + Italic / Bold + Underline / Bold + Italic + Underline? Or just one? Because at the moment if first 2 conditions are true then second `if` will override first `if`. – Tomas Jablonskis Apr 05 '18 at 12:48
  • Depends on the selected checkbox (if Bold an Italic selected then text should be Bold and Italic etc). But still no idea why only two of them are working. Previously all of them were working and code was exactly the same. – Karol Apr 05 '18 at 12:51
  • Then as I mentioned above, if Bold conditional and Italic conditional are both `true` then second conditional will always override first one. Just use this in second `if`: `tx.setTypeface(null, Typeface.ITALIC | Typeface.BOLD);` – Tomas Jablonskis Apr 05 '18 at 12:54
  • But now when you will only choose checkBox Italic then also Bold will be activated – Karol Apr 05 '18 at 12:58
  • Well that is logical. My bad. In that case try adding one more condition between Italic and Undeline: `if (loadCheck1 && loadCheck2) { tx.setTypeface(null, Typeface.BOLD_ITALIC); }` and revert my previous suggestion. – Tomas Jablonskis Apr 05 '18 at 13:03
  • Ok, thanks that worked. 3 additional if's were needed: `if (loadCheck1 && loadCheck2) { tx.setTypeface(tx.getTypeface(), Typeface.BOLD_ITALIC); } else if (loadCheck1 && !loadCheck2) { tx.setTypeface(tx.getTypeface(), Typeface.BOLD); } else if (!loadCheck1 && loadCheck2) { tx.setTypeface(tx.getTypeface(), Typeface.ITALIC); }` – Karol Apr 05 '18 at 13:13