How to initialize switch inside a fragment?

I need to provide some interaction to the user using a switch in a fragment. My switch is called protectionSwitch, and I need to set an OnCheckedChangedListener, to my switch.
Can anyone help on how to do this?

I want you to write complete code for me. Thanks for your support!
Yours,
DeveloperSN

Why don’t you show us what you already have and where the problem is?

1 Like

I have set up a navigation drawer that needs fragments - now I need one of my fragments to have a slider switch (just a switch) that stores a Boolean value to the SharedPreferences key. Now all I need to know is how I can initialize the switch inside the Fragment.

I have tried to initialize, it in the onCreateView method of the Activity extending the Fragment, but I get an error: “Unreasonable statement”, which basically means that I can’t use it there. How should I do it? If I use the one I tried, then I get a NullPointerException. Even using a contract statement doesn’t help, as I guess I’m referencing a null object as there’s a difference way of initializing it.

Got it fixed. I had forgotten to put the RootView in. It’s fixed now.
I love you all!

Yours,
Sayan