Question

I'm new to Android Preferences which I am using for a settings menu and I just had a few questions. I looked on the API site and couldn't find a way to add action to them. I have an Activity:

public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}

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

        PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
        addPreferencesFromResource(R.xml.preferences);
    }
}
}

which works fine, it displays my xml PreferenceScreen page which contains 4 Preference tags. My question is how do I add action when clicking on those preferences. For example I want a separate pop up window to be displayed where I can change a number value and for that to be saved each time I open the app. If someone could provide an example or something I would really appreciate it

Was it helpful?

Solution

I want a separate pop up window to be displayed where I can change a number value and for that to be saved

This is easy, you just have to make that Preference an EditTextPreference at your xml file like so:

<EditTextPreference 
 android:title="@string/title"
 android:key="preferenceKey" />

You can customize it more: if you only want integers android:numeric="integer" , if you want to set the max length android:maxLength="3" and the default value android:defaultValue="10" . You don't have to do anything in the Java class


how do I add action when clicking on those preferences

If you want to add some more complicated action, use a Preference.OnPreferenceClickListener. You can use it like so:

Preference preference = // some preference
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
    @Override
    public boolean onPreferenceClick(Preference p){
        //do something
        return false;
    }
});

You can find more at this question. Hope this helps

PS. If you want to customize the preference a lot (i.e. not only the action onClick, but also the layout etc), you should consider creating a custom Preference, as said at another answer.

OTHER TIPS

It sounds like what you are trying to do would be appropriately handled by creating a custom Preference. The Android docs has a decent tutorial on this: http://developer.android.com/guide/topics/ui/settings.html#Custom

Based on your requirement, you can work through intents for your preference tags. below is my example code in Menu.java: and also you can follow the developer tutorial at : http://developer.android.com/guide/topics/ui/settings.html#Custom

public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
            case R.id.about:
                /*Intent aboutIntent = new Intent(Menu.this, About.class);
                startActivity(aboutIntent);*/

                Intent aboutIntent = new Intent("com.example.myfirstapp.ABOUT");
                startActivity(aboutIntent);
            break;
            case R.id.preference:
                Intent prefsIntent = new Intent("com.example.myfirstapp.PREFS");
                startActivity(prefsIntent);
            break;
            case R.id.exit:
                finish();
                break;
        }
        return false;
    }

Code in About.java

public class About extends Activity {
    TextView TV1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        TV1 = (TextView) findViewById (R.id.textView1);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top