5

I was following this documentations to create a setting page. I have created two java classes based on the documentation:

SettingsFragment.java:

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
    ...
}

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }
}

In the preference I have an item with key premium_support which I want to create a click listener for it.

 <CheckBoxPreference
        android:key="@string/premium_support"
        android:title="Premium Support"
        android:summary="Purchase premium support"
        android:defaultValue="false" />

I can't create a click listener in the fragment because I have my in app billing codes in the SettingsActivity. I tried this answer but seems like findPreference is only for the preferencefragment.

Any idea how to implement the click listener in SettingsActivity?

Community
  • 1
  • 1
Rain Man
  • 1,163
  • 2
  • 16
  • 49

2 Answers2

6

In the onCreate() method of SettingsActivity:

Preference preference = findPreference("prefs_key");
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
   @Override
   public boolean onPreferenceClick(Preference preference) {
       Toast.makeText(SettingsActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
       return true;
   }
});
Darush
  • 11,403
  • 9
  • 62
  • 60
1

The 'findPreference' method can only be called on the settingsfragment because it extends preferencefragment. You need to get a reference to the settingsfragment class in the settings activity like below:

    private SettingsFragment settingsFragment;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("SETTINGS");

         //instantiate the settings fragment
         settingsFragment= new SettingsFragment();
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, settingsFragment)
                .commit();
    }

You then override onResume and set the preferenceclicklistener in onresume and not oncreate because the commit() method is called asynchronously ( i.e the code does not execute immediately) and the preference value might be null if you call findpreference immediately after commiting the fragment transaction in oncreate.

    @Override
        protected void onResume() {
            super.onResume();
            Preference preference = 
          settingsFragment.findPreference(getString(R.string.pref_key));
            preference.setOnPreferenceClickListener(new 
            Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                   //respond to click events here
                    return true;
                }
            });

        }
Oluwasegun Wahaab
  • 2,663
  • 3
  • 16
  • 19