material-animated-switch icon indicating copy to clipboard operation
material-animated-switch copied to clipboard

How to set the init state

Open zyxq opened this issue 8 years ago • 7 comments

is the any method to set init state?

zyxq avatar Aug 03 '15 09:08 zyxq

Yes how can we set initial state?

hongzhou85 avatar Aug 04 '15 07:08 hongzhou85

In the onCreate(), Calling post before toggle is required, otherwise you will run into NULL error. I believe it may not be initialized.

togglebtn.post(new Runnable() { @Override public void run() { if (sharedPref.getBoolean(preference, false)) { if (!togglebtn.isChecked()) { togglebtn.toggle(); } } } });

hongzhou85 avatar Aug 04 '15 08:08 hongzhou85

@hongzhou85 is right. If you call the toggle method in your Activity's onCreate() callback you get a NullPointerException. Currently the runnable seems to be the only working solution (calling the toggle method in the onResume() callback isn't appropriate, imho).

I did my own custom class which extends MaterialAnimatedSwitch and sets the initial state.. but the developer of this pretty library should take care of it.

andrerinaldi avatar Sep 14 '15 10:09 andrerinaldi

I'm getting stuck with this as well, I hope the developer will manage to fix this as soon as possible. @andrearinaldispotsoftware, you said you managed to extend MaterialAnimatedSwitch and set the initial state. I tried to do that as well but... how did you set the initial state in the extended class?

MarKco avatar Oct 20 '15 15:10 MarKco

I'm Having the same problem. It's too bad we have to call to the runnable each time we want to init the switch... hope it will be fixed soon :\

moheny avatar Nov 24 '15 12:11 moheny

I also hope it will be fixed ASAP

Andecy avatar Mar 03 '16 09:03 Andecy

I also hope it will be fixed ASAP

Yanruplus avatar Jun 18 '22 12:06 Yanruplus