Search code examples
androidandroid-fragmentssharedpreferencesandroid-preferencespreferencefragment

Cannot get view from preferencefragment


I have the following preference fragment xml:

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

    <PreferenceScreen
        android:order="1"
        android:summary="Sound settings"
        android:title="Sound"
        android:widgetLayout="@layout/preference_switch_sound" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.test.SoundPreferencesActivity"
            android:targetPackage="com.test" />
    </PreferenceScreen>

</PreferenceScreen>

And my sound switch widgetLayout that I point to above:

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/soundSwitchWidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="16dip"
    android:focusable="false"
    android:clickable="true" />

My problem is that I need to grab the switch in code such that I can attach the onClickListener.

I have tried finding the view in my Preference Activity:

public class PublicPreferencesActivity extends PreferenceActivity {

... 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, preference).commit();
            Switch s = (Switch) findViewById(R.id.soundSwitchWidget);
                // I always get null here
    }

}

I have also tried in my fragment:

public static class PublicPreferenceFragment extends PreferenceFragmentSummary  {       
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.publicpreferences);
                    Switch s = (Switch) findViewById(R.id.soundSwitchWidget);
                    // switch is null here too

    }
}

How do I get access programmatically to the switch I put in the preference screen?

EDIT: There was a question about why I am not using a SwitchPreference. Basically I want to be able to turn sound on/off with the switch but also be able to switch to a new PreferenceScreen if they click in the preference (not on the switch, but the title). I tried to use a SwitchPreference however when I click on the text it switches the switch and sends me to the the PreferenceScreen, rather than just send my to the PreferenceScreen.

I am trying to mimic the wifi behavior in main setting. I.E. you can turn on/off the switch from the top level, but also click the text to visit a more detailed screen.


Solution

  • Any particular reason you aren't using a SwitchPreference?

    <SwitchPreference
        android:order="1"
        android:summary="Sound settings"
        android:title="Sound" >
       <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.test.SoundPreferencesActivity"
            android:targetPackage="com.test" />
    </SwitchPreference>
    

    But as far as calling View.findViewById for your Switch, you'd need to subclass Preference, as you can't subclass PreferenceScreen, then override Preference.bindView and initialize it from there.