Search code examples
androidxmlpreferencesandroid-preferencessharedpreferences

Android: Using the same preference several times in a preferences xml


I have an EditTextPreference that I want to show to the user in two different PreferenceScreens. It is supposed to be the exact same preference (let the android:key be "myEditText"), just shown on two different screens. So, here's what it could look like (totally stripped down to show you an example):

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="@string/someCategory">
        <PreferenceScreen android:key="someScreen">
            <PreferenceScreen android:key="someSubScreen">
                <PreferenceCategory android:title="@string/someSubCategory">
                    <EditTextPreference android:key="myEditText"
                    ....
                    />
                </PreferenceCategory>
            </PreferenceScreen>
        </PreferenceScreen>
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/someOtherCategory">
        <PreferenceScreen android:key="someOtherScreen">
            <PreferenceScreen android:key="someOtherSubScreen">
                <PreferenceCategory android:title="@string/someOtherSubCategory">
                    <EditTextPreference android:key="myEditText"
                    ....
                    />
                </PreferenceCategory>
            </PreferenceScreen>
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

When I go to myEditText via someScreen and enter a text, it is not shown when I go back and open myEditText via someOtherScreen. I have to close the preferences and open it again. Then, both EditTexts show the same text.

So, what would be the best way to have the exact same Preference on different screens?

/Edit:

Why do I want to do that, anyway? The first PreferenceScreen is all about URL shortening. You can set options for shortening, select / reorder services you want to use and enter user credentials for bit.ly, goo.gl etc.

The second screen is for file upload services where you can do the same (setting options for file upload, selecting / reordering services, entering user credentials for upload services).

Now I'm integrating CloudApp which is both, a file AND a URL shortening service. So, the user might be epecting to find it on the URL shortening screen but he might also look for it at the file upload screen. So, I want to make it easy for him and just show it on both screens. They are not top-level screens, it's quite a deep structure.

So, besides this making sense or not: Is it possible?


Solution

  • Okay, here's what I'm doing now. It's not pretty, but it does the job and at the moment I'm content with that:

    Instead of using the same key, use different EditTextPreferences (say myEditText and myEditText2). In the preference code create an OnPreferenceChangeListener:

        final OnPreferenceChangeListener myEditTextChangeListener = new OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String newEntry = (String)newValue;
                myEditText.setText(newEntry);
                myEditText2.setText(newEntry);
                return false;
            }
        }; 
    

    Assign the Listener to both EditTextPreferences:

    myEditText.setOnPreferenceChangeListener(myEditTextChangeListener);
    myEditText2.setOnPreferenceChangeListener(myEditTextChangeListener);
    

    In the main activity where the OnSharedPreferenceChangeListener is registered, I only need to monitor changes to the first EditText. In fact, I totally ignore the second EditText because it always has the same entry as the first EditText.