How to update my project by creating a multi-user application

I would like to make it possible SettingsActivityfor the user to personalize the appearance of the application. In this exercise, the user can save the application in a "light theme" (which means, for example, a white background with black texts) or a "dark theme", the opposite colors of the light theme in favor of night use.

How can I do that?

I was thinking of creating different layouts in xml for each topic.

edits

Below are examples SettingsActivity, I would like to change the appearance for the entire application, and not a single activity.

enter image description here

enter image description here

enter image description here

enter image description here

+4
source share
4 answers

. , .

styles.xml :

     <!-- Use this theme in Manifest by default -->
    <style name="MyLightTheme" parent="Base.AppTheme.Light"></style>
    
    <!-- Base light theme containing all styles -->
    <style name="Base.AppTheme.Light" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        ... Other styles
    </style>

    <!-- Use this to switch to Dark theme -->
    <style name="MyDarkTheme" parent="Base.AppTheme.Dark"></style>

    <!-- Base dark theme containing all styles -->
    <style name="Base.AppTheme.Dark" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        ... Other styles
    </style>

, PreferenceFragment.

PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);

onSharedPreferenceChanged():

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.equals(getString(R.string.pref_key_nighttheme))) {

            if (sharedPreferences.getBoolean(getString(R.string.pref_key_nighttheme), false)) {
                //  Night theme enabled

                getActivity().setTheme(R.style.MyDarkTheme);  
                       getActivity().getApplication().setTheme(R.style.MyDarkTheme);
               darkTheme = true;

            } else {
                getActivity().setTheme(R.style.MyLightTheme);
                getActivity().getApplication().setTheme(R.style.MyLightTheme);
               darkTheme = false;
            }

            getActivity().recreate(); // This is important. It allows the theme change to take effect.
        } 
    }

MainActivity onResume(), MainActivity.


, , super() onCreate().

  isThemeDark = setDarkTheme(this);

setDarkTheme() - , , SharedPreference. , .

    public static boolean setDarkTheme(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    boolean isDarkTheme = prefs.getBoolean(context.getString(R.string.pref_key_nighttheme), false);
    context.setTheme(SettingsActivity.darkTheme ? R.style.MyDarkTheme : R.style.MyLightTheme);

    return isDarkTheme;
}

, Newslet:

enter image description here

UPDATE: Android AppCompat DayNight Theme. tutorial .

+1

, , , , , .

getApplication(). setTheme (Theme.Holo)

+2

, fooobar.com/questions/127650/...

public class BaseActivity extends Activity {

    protected void onCreate(Bundle state) {
        super.onCreate(state);
        String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("theme", "black");
        setTheme(getTheme(theme);
    }

    private int getTheme(String theme) {
        if (theme.equals("black") return R.style.ThemeBlack;
        if (theme.equals("white") return R.style.ThemeWhite;
        ...
        return android.R.style.Theme; // stub
    }
}

BaseActivity. PreferenceActivity PreferenceFragment, , - Activity (AppCompatActivity) PreferenceActivity

P.S. R.style.ThemeBlack - styles.xml

<style name="ThemeBlack" parent="android:Theme.Holo">
</style>
+2
  • xml-.

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColorAmber</item>
        <item name="colorPrimaryDark">@color/primaryDarkColorAmber</item>
        <item name="colorAccent">@color/secondaryColorAmber</item>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    <style name="AppTheme.RED" parent="AppTheme.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColorRed</item>
        <item name="colorPrimaryDark">@color/primaryDarkColorRed</item>
        <item name="colorAccent">@color/secondaryColorRed</item>
    </style>
    
    <style name="AppTheme.PINK" parent="AppTheme.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColorPink</item>
        <item name="colorPrimaryDark">@color/primaryDarkColorPink</item>
        <item name="colorAccent">@color/secondaryColorPink</item>
    </style>
    
  • , onCreate() setContentView().

    // , .

    int theme = getThemeFromPreferences(); // R.style.AppTheme_RED
    setTheme(theme);
    
  • / ( ), , .

    // save your theme id

    saveThemeInPreferences(R.style.AppTheme_RED);
    //recreate this activity to see changes
    SettingActivity.this.recreate();
    

For more detailed and sample code ... take a look at the Multithreaded Android implementation

0
source

Source: https://habr.com/ru/post/1649592/


All Articles