2
votes

I have implemented android Day/Night theme in my application and for some reasons.

1) I am unable to change the color of my text and also for making it work I have to click on toggle button and go back to main screen, that means the implementation is not showing in the settings page and have to be refreshed by going to Homepage

2) The second problem I am facing is that when the day/night theme is applied my recyclerView posts get back to 1st in list and do not just get applied where it was.

Please help!!.

I am using Android studio

Day/Night theme class file:

public class settings extends AppCompatActivity {

    private ToggleButton DayNightt;
    private static Bundle bundle = new Bundle();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_page);
        DayNightt = (ToggleButton) findViewById(R.id.dayNight_Switch);
        DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);


                }
            }
        });
}
    @Override
    public void onPause() {
        super.onPause();
        bundle.putBoolean("ToggleButtonState", DayNightt.isChecked());
    }
    @Override
    public void onResume() {
        super.onResume();
        DayNightt.setChecked(bundle.getBoolean("ToggleButtonState",false));

    }
}

color-night xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimaryNight">#222222</color>
    <color name="colorPrimaryDarkNight">#222222</color>
    <color name="colorAccentNight">#FF4081</color>
    <color name="textColorPrimaryNight">#FFFFFF</color>
    <color name="colorBackgroundNight">#000000</color>
</resources>

style-night xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimaryNight</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
    <item name="colorAccent">@color/colorAccentNight</item>
    <item name="android:windowBackground">@color/colorBackgroundNight</item>
        <item name="android:textColorPrimary">@color/textColorPrimaryNight</item>

    </style>
    </resources>

Style xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/textColorPrimary</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

color xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#824c83dd</color>
    <color name="textColorPrimary">#000000</color>
    <color name="colorPrimaryDark">#303f9f</color>
    <color name="colorAccent">#FF4081</color>
    <color name="SourceColor">#00bdbdbd</color>
    <color name="background">#FFFFFF</color>
</resources>
2
This is a question about code in android studio, not about android studio. DOn't use the Android Studio tag for questions about code in AS - Zoe

2 Answers

1
votes

I have implemented the color correction part successfully by adding the following code:

colors xml file:

<color name="TextP">#000000</color>

color-night xml file:

<color name="TextP">#FFFFFF</color>

And then assigning my text the same color as above in colors.xml

but rest of parts are still a problem to me so if anybody can help, please.

0
votes

What about adding in color-night.xml this string that you have used in normal color.xml? <color name="textColorPrimary">#000000</color>