AndroidX - Simulate Preference Click Programmatically

338 views Asked by At

I am in the process of changing my Android project from android.preference to androidx.preference. Previously in my project, I was launching a DialogPreference programmatically using the following method:

public class WaterbodyPreferenceActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        addPreferencesFromResource(R.xml.preferences_waterbody);
        // launch DialogPreference from PreferenceScreen
        PreferenceScreen screen = (PreferenceScreen) findPreference("pref_key_waterBodyInformation");
        waterbodyPickerPreference = (WaterbodyPickerPreference) findPreference("pref_waterBodyName");
        int pos = waterbodyPickerPreference.getOrder();
        screen.onItemClick( null, null, pos, 0 );
    }
}

This was working well. Unfortunately, the screen.onItemClick method is no longer available and I have been unable to find any way to programmatically launch a Preference.

Here is a more full picture of what I am trying to do.

I have a Fragment with several elements, including a button that I would like to have launch a DialogPreference.

As I stated, when using the android.preference library, the button would launch a PreferenceActivity, which would then programmatically click a Preference and launch the DialogPreference using the code above.

I've tried creating a show() method within my DialogPreference, and calling it from the PreferenceFragmentCompat, but this doesn't seem to have any effect:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences_waterbody);
    WaterbodyPickerPreference dialogPreference = (WaterbodyPickerPreference) findPreference("pref_waterBodyName");
    if (dialogPreference != null) { dialogPreference.show(); }
}

DialogPreference method:

public void show() {
    onClick();
}

The Fragment layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/main_view"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/wb_waterbody"
            android:padding="5dp"
            android:textSize="14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <!-- the button that should launch the DialogPreference -->
        <Button
            android:id="@+id/btn_waterbody"
            android:text="@string/pref_waterBodyName"
            android:background="@color/white"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/btn_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >

        <Button
            android:background="@android:drawable/btn_default"
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_cancel" />

        <Button
            android:background="@android:drawable/btn_default"
            android:id="@+id/btn_done"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_done" />

    </LinearLayout>

</RelativeLayout>

preferences_waterbody.xml, which I do not want to show, but simply click through to launch the DialogPreference.

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="pref_key_waterBodyInformation">

    <org.lakeobserver.observer.android.preference.WaterbodyPickerPreference
        android:key="pref_waterBodyName"
        android:dialogTitle="@string/waterbody_picker_title" />

</androidx.preference.PreferenceScreen>

The Fragment code that launches the PreferenceActivity:

btnWaterbody.setOnClickListener(view -> {
    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .replace(android.R.id.content, new WaterbodyPreferenceActivity())
            .commit();
});

Updated PreferenceActivity:

public class WaterbodyPreferenceActivity extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences_waterbody);
        // launch DialogPreference...?
    }
}
1

There are 1 answers

0
brassmookie On BEST ANSWER

It was actually quite simple.

public class WaterbodyPreferenceActivity extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences_waterbody);
        WaterbodyPickerPreference dialogPreference = (WaterbodyPickerPreference) 
        findPreference("pref_waterBodyName");
        onDisplayPreferenceDialog(dialogPreference);
    }
}