0

Ok, so the thing is that i'm trying to get my android application to have a settings activity for the user to be able to change some of the application's features (language, theme, ...). My problem comes when trying to get the app to react to the change on the value of one of those preferences. For instance the theme one; my idea would be to have a "Switch preference". When it would be on, the app's theme would be Material.Light and when off, Material. For this I though to have some "onValueChanged" method that would react when the switch changed its position. The problem here is that I'm unable to properly get an instance of the SwitchPreference in my SettingsActivity, both because the "findPreference(key)" method is deprecated and I don't really know how to make it take the value of the needed key.

There is any way to do this, or should I change the way of thinking for this problem?

1 Answers1

0

Instead of setting the theme in onValue change listener, you can also do like below:

1) Create ThemeUtils class and do switch case for theme selection and set a theme for activity. I created own style, you can select your stlye name here

public class ThemeUtils {
public final static int THEME_Professional = 1;
public final static int THEME_Default = 2;
public static void onActivityCreateSetTheme(Activity activity, int sTheme) {
    switch (sTheme)
    {
    default:
    case THEME_Professional:
        activity.setTheme(R.style.ProfessionalTheme);
        break;
    case THEME_Default:
        activity.setTheme(R.style.DefaultTheme);
        break;
    }
}

}

2)In onCreate method of Activity, call setTheme method

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme();
  }

3)Gets user selected theme

   private void setTheme() {
    int themeInt = getThemeValue();
    ThemeUtils.onActivityCreateSetTheme(this, themeInt);
   }

4) After getting the style id, you can call ThemeUtils class method onActivityCreateSetTheme

private int getThemeValue() {
    SharedPreferences pref =     PreferenceManager.getDefaultSharedPreferences(getContext());
    Boolean isGrid = pref.getBoolean(getString(R.string.grid_switch), false);
    if (isGrid) {
        return 2;
    } else {
        return 1;
    }
}

Hope this option helps you !!!

kavita
  • 417
  • 4
  • 8
  • I'm trying to implement the code, to see how it works and see if it gets the job done, but I get an error; what does the "R.string.grid_switch", corresponds to? – Silvan Zamora Feb 15 '17 at 18:47
  • It is switch preference key. – kavita Feb 15 '17 at 18:55
  • Anyways, if i understand correctly the code (it is possible that I do not... XD), this solves half of the problem. The thing is that here you check the selected theme on the onCreate method of the Activity. In that case, it would be needed to destroy the activity and recreate it, in order to have the change active. For me, the optimal solution would be to get the change right after the switch changes its position. That said, if its not possible, I can end up putting something like "the changes will take effect after the app is restarted", -- continues after -- – Silvan Zamora Feb 15 '17 at 18:56
  • -- continues here -- And solve it like that (XD). It's not optimal, but it's possible... – Silvan Zamora Feb 15 '17 at 18:58
  • " It is switch preference key." ... i should have thought about that. My bad. :( – Silvan Zamora Feb 15 '17 at 18:59
  • After selecting the theme option in Settings, you will be back to activity only. You can also call this setTheme in onStart method of activity. @Override protected void onStart() { super.onStart(); setTheme();} – kavita Feb 15 '17 at 18:59