I have a preference screen having multiple SwitchpreferenceCompat. I need to give a user choice of theme change,vibration,etc in settingActivity. Can any one help me in how to set vibrate or theme change by clicking switch in settingactivity. I used this inside my settingActivity fragment but when i click setting activity the app stops
SwitchPreferenceCompat switchPref = findPreference("key");
switchPref.setChecked(true);
`res/xml/root_preference.xml
<SwitchPreferenceCompat
android:id="@+id/vibrate"
app:key="On"
app:title="Vibrate"
android:summaryOff="Off"
android:summaryOn="On"
app:icon="@drawable/ic_baseline_vibration_24"
android:defaultValue="true" />
<SwitchPreferenceCompat
app:key="On"
app:title="Sound"
app:summaryOff="Off"
app:summaryOn="On"
app:icon="@drawable/ic_baseline_volume_up_24"
android:defaultValue="true"/>
</PreferenceCategory>
User clicks the vibration on the app must vibrate when the button is clicked in the activity. How can i do this?

Use Androidx library
Then create
xmlfolder and add yourSwitchPreferenceCompatlike below:Then create
SwitchPreferenceCompatActivitylike below:}
Note: add
<uses-permission android:name="android.permission.VIBRATE" />permission on manifest file.