glomadrian / material-animated-switch

A material Switch with icon animations and color transitions
1.18k stars 158 forks source link

How to set the init state #11

Open zyxq opened 9 years ago

zyxq commented 9 years ago

is the any method to set init state?

hongzhou85 commented 9 years ago

Yes how can we set initial state?

hongzhou85 commented 9 years ago

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(); } } } });

andrerinaldi commented 8 years ago

@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.

MarKco commented 8 years ago

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?

moheny commented 8 years ago

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 :\

Andecy commented 8 years ago

I also hope it will be fixed ASAP

Yanruplus commented 2 years ago

I also hope it will be fixed ASAP