How to initialize switch inside a fragment?

How to initialize switch inside a fragment?
0

#1

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


#2

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


#3

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:


#4

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.


#5

Like @BenGitter suggested above, can you please provide a link to your current code, so we can see what you have so far? Then, we can take a look and offer suggestions based on what you have detailed out above.


#6

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

Yours,
Sayan