Use Switch button to toggle dayNight theme

与世无争的帅哥 提交于 2021-02-08 03:54:08

问题


I have implemented a Switch button in my layout and want to use Android dayNight theme using the button, dayNight theme works okay but the problem is that when ever I click the switch it does not work instantly, i have to change activities and then it works, for example if I click on switch in one activity it will not do anything until i press the back button and move to some other activity and again come back to that activity and also my switch state always set to default when ever I change my activity and comeback, please help

I am using Android Studio with dayNight theme

Below is my Switch Activity

   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        setContentView(R.layout.settings_page);
        DayNightt = (Switch)findViewById(R.id.dayNight_Switch);
        DayNightt.setChecked(false);
        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);
                }
            }
        });
        super.onCreate(savedInstanceState);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

XML Switch Activity

<Switch
        android:id="@+id/dayNight_Switch"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="58dp"
        android:checked="false"
        android:text="Switch" />

回答1:


after change the switch add this method:

super.recreate();

This recreates the activity and if work fine sets the correct theme.

Some like that:

DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    super.recreate();
                    //Or this
                    recreate();

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    super.recreate();
                    //Or this
                    recreate();
                }
            }
        });

UPDATE:

If the first method not Works you can try this:

DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    Intent intent = getActivity().getIntent();
                    getActivity().finish();
                    startActivity(intent);

                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    Intent intent = getActivity().getIntent();
                    getActivity().finish();
                    startActivity(intent);
                }
            }
        });

note: this second way for me is not the best but you can try.




回答2:


I have solved my switch button state change by implementing onPause() and onResume () methods in my activity, Now the switch state remains the same after changing activities, I have also implemented a bundle in my Activity. I have added it after onCreate method.

Updated code

 private static Bundle bundle = new Bundle();

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

only problem left is I have to switch activity to see it implementing, will get over it in sometime.



来源:https://stackoverflow.com/questions/45378151/use-switch-button-to-toggle-daynight-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!