I've got a SettingsFragment with a SwitchPreferenceCompat-Control
<SwitchPreferenceCompat
app:key="my_key"
app:title="@string/prefs_my_title" />
Background: I'm using https://github.com/worker8/TourGuide and want to lead the user through the necessary settings - So how can I get a handle (must be a view) on my switch to pass it to Tourguide?
I already tried to get a handle by
<SwitchPreferenceCompat
android:id="@+id/myId"
app:key="my_key"
app:title="@string/prefs_my_title" />
getView().findViewById(R.id.myId)
But since getView() returning null I cannot get any further with this approach.
How can I get/pass the particulary views from SwitchPreferenceCompat or EditTextPreference from inside the SettingsActivity/-Fragment?
Any help would be welcome - if someone knows a better tool than tourguide - especially for showing preferences - please also let me know.
Update:
public void onResume() {
View view = getView();
// This should be my SwitchPreferenceCompat
View targetView = view.findViewById(R.id.myId);
Log.d("view", ""+view);
Log.d("viewId", ""+view.getId());
Log.d("targetView", ""+targetView);
super.onResume();
}
Brings up the following:
D/view: android.widget.LinearLayout{1afdad5 V.E..V... ......ID 0,0-0,0}
D/viewId: -1
D/targetView: null
So I now got a view, but it doesn't seem to be the one I defined, the ID of the linear Layout ist not the one from the settings_activity.xml (I cannot get any other view from the xml by ID)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res /android"
android:id="@+id/linearId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/test"
android:layout_width="100dp"
android:layout_height="100dp"></Button>
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and the SwitchPreference is also not found by ID. Any ideas so far? Since this threads gets longer -> if someone got a simple example how to fetch the view of a dynamically generated SwitchPreferenceCompat-Control -> you're welcome, also are you Zain and thank you very much for your help!