Search code examples
androidlistviewpreferences

A listview item in preferences


I'm new in Android app development! I'm developing an app with settings. I want to divide the settings in categories (like in android system settings). I found preferences, and in particular this guide: http://android-journey.blogspot.it/2010/01/for-almost-any-application-we-need-to.html So I create my activity and the xml file, no problem. But I want to add an item like listview that when I touch it, it opens another activity. Is it possible to do?


Solution

  • You should to create settings with preferences - items of your "listview" and set actions to them: Intents to your another settings activities.

    private void createRateAppPrefListener() {
    
        Intent marketIntent = getMarketIntent(mContext);
        String rateAppKey = getString(R.string.pref_rate_app_key);
        Preference rateAppPref = findPreference(rateAppKey);
        if (rateAppPref != null) {
            rateAppPref.setIntent(marketIntent);
        }
    }
    
    private Intent getMarketIntent(Context context) {
        String appPackageName = context.getPackageName();
        Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
        marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        return marketIntent;
    }
    

    shortly

    Intent intent = ...
    Preference pref = findPreference(getString(R.string.your_pref_key));
    pref.setIntent(intent);
    

    Youtube preference, for example example screenshots

    UPDATE: It is not a listview or listitem it is standart preference, defined in xml file

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <Preference
            android:key="@string/pref_rate_app_key"
            android:summary="@string/pref_rate_app_summary"
            android:title="@string/pref_rate_app_title" />
    
     ...
    

    But it can be simpler

        <Preference
            android:summary="@string/pref_licenses_summary"
            android:title="@string/pref_licenses_title">
    
                <intent android:action="com.mdhelper.cardiojournal.view.activities.LicensesActivity" />
    
        </Preference>
    

    Just add intent definition in your preference xml