Preference Subtitles

Stay organized with collections Save and categorize content based on your preferences.

For the most part, adding preference summaries is relatively straightforward as it involves just adding the android:summary attribute to the respective preference with the appropriate string resource. However, if the subtitles should update dynamically then a custom preference controller may be necessary.

Static subtitles

To add a static subtitle to a preference:

  1. Add the android:summary attribute to the preference. For example, to add a summary to the L0 display settings preference, add something like the following to the preference attributes:

    For example, consider this full preference code sample:


Dynamic subtitles

Subtitles specified with the android:summary attribute are static so cannot update based on certain conditions. For dynamic subtitles, you need to modify the preference controller for the preference. The following example modifies the L0 location preference to have a subtitle that specifies if location is on or off and, if on, state how many apps currently have location access.

  1. Define the new strings:
    <!-- Summary for Location settings when location is off [CHAR LIMIT=NONE] -->
        <string name="location_settings_summary_location_off">Off</string>
        <!-- Summary for Location settings when location is on, explaining how many apps have location permission [CHAR LIMIT=NONE]-->
        <plurals name="location_settings_summary_location_on">
            <item quantity="one">On - <xliff:g id="count">%1$d</xliff:g> app has access to location</item>
            <item quantity="other">On - <xliff:g id="count">%1$d</xliff:g> apps have access to location</item>
        <!-- Location settings, loading the number of apps which have location permission [CHAR LIMIT=30] -->
        <string name="location_settings_loading_app_permission_stats">Loading\u2026</string>
  2. Create a new PreferenceController, LocationEntryPreferenceController, to dynamically set and change the location preference summary text:
    public class LocationEntryPreferenceController extends PreferenceController<Preference> {
        private static final Logger LOG = new Logger(LocationEntryPreferenceController.class);
        private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
                new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
        private final Context mContext;
        private final LocationManager mLocationManager;
        /** Total number of apps that have location permissions. */
        private int mNumTotal = -1;
        private int mNumTotalLoading = 0;
        private AtomicInteger mLoadingInProgress = new AtomicInteger(0);
        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
        public LocationEntryPreferenceController(Context context, String preferenceKey,
                FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
            super(context, preferenceKey, fragmentController, uxRestrictions);
            mContext = context;
            mLocationManager = (LocationManager) getContext().getSystemService(
        protected Class<Preference> getPreferenceType() {
            return Preference.class;
        protected void onStartInternal() {
            getContext().registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED);
        protected void onStopInternal() {
        protected void updateState(Preference preference) {
            if (!mLocationManager.isLocationEnabled() || mLoadingInProgress.get() != 0) {
            mNumTotalLoading = 0;
            // Retrieve a list of users inside the current user profile group.
            List<UserHandle> users = mContext.getSystemService(
            for (UserHandle user : users) {
                Context userContext = createPackageContextAsUser(mContext, user.getIdentifier());
                if (userContext == null) {
                    if (mLoadingInProgress.decrementAndGet() == 0) {
                        setLocationAppCount(preference, mNumTotalLoading);
                PermissionControllerManager permController =
                        Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
                        (numApps) -> {
                            mNumTotalLoading += numApps;
                            if (mLoadingInProgress.decrementAndGet() == 0) {
                                setLocationAppCount(preference, mNumTotalLoading);
                        }, null);
        void setLocationAppCount(Preference preference, int numApps) {
            mNumTotal = numApps;
        private void updateSummary(Preference preference) {
            String summary = "";
            if (mLocationManager.isLocationEnabled()) {
                if (mNumTotal == -1) {
                    summary = mContext.getString(R.string.location_settings_loading_app_permission_stats);
                } else {
                    summary = mContext.getResources().getQuantityString(
                            mNumTotal, mNumTotal);
            } else {
                summary = mContext.getString(R.string.location_settings_summary_location_off);
        private Context createPackageContextAsUser(Context context, int userId) {
            try {
                return context.createPackageContextAsUser(
                        context.getPackageName(), 0 /* flags */, UserHandle.of(userId));
            } catch (PackageManager.NameNotFoundException e) {
                LOG.e("Failed to create user context", e);
            return null;

    For this sample controller:

    • If location is disabled, summary text is set to the location_settings_summary_location_off string.
    • If location is enabled, the number of apps that have location permission is added. While this is loading, the location_settings_loading_app_permission_stats string is shown. When the data is loaded, the controller sets the summary to the location_settings_summary_location_on string with the number of apps with access specified.
    • When the preference controller is started, the controller registers a receiver and refreshes the preference state when the location state changes.
  3. Modify the fragment XML file to attach the new controller to the relevant preference: