Search code examples
androidandroid-activityandroid-preferencespreferenceactivity

Launch new activity from PreferenceActivity


Good day, friends. I have a PreferenceActivity, it is filled from XML file. When we press one item, we should launch new activity. How to do it? What should I write in XML-file or in the Java-class?


Solution

  • After you add preferences using

    addPreferencesFromResource(R.xml.preferences);
    

    find your preference that you want to set onClick using

    findPreference("foo_bar_pref");
    

    and define it by casting like

    Preference fooBarPref = (Preference) findPreference("foo_bar_pref");
    

    Then you can easily set its onClick using

    fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}
    

    You can start your new Activity (using an Intent) inside that listener.