Code example for PreferenceCategory

Methods: removePreference

0
        boolean hasTtsPrefs = populateOrRemoveTtsPrefs();
        if (!hasRecognizerPrefs && !hasTtsPrefs) {
            // There were no TTS settings and no recognizer settings, 
            // so it should be safe to hide the preference category 
            // entirely. 
            mFragment.getPreferenceScreen().removePreference(mVoiceCategory);
        } 
    } 
 
    private boolean populateOrRemoveRecognizerPrefs() { 
        List<ResolveInfo> availableRecognitionServices =
                mFragment.getPackageManager().queryIntentServices(
                        new Intent(RecognitionService.SERVICE_INTERFACE),
                        PackageManager.GET_META_DATA);
        int numAvailable = availableRecognitionServices.size();
 
        if (numAvailable == 0) {
            mVoiceCategory.removePreference(mRecognizerPref);
            mVoiceCategory.removePreference(mRecognizerSettingsPref);
            return false; 
        } 
 
        if (numAvailable == 1) {
            // Only one recognizer available, so don't show the list of choices, but do 
            // set up the link to settings for the available recognizer. 
            mVoiceCategory.removePreference(mRecognizerPref);
 
            // But first set up the available recognizers map with just the one recognizer. 
            ResolveInfo resolveInfo = availableRecognitionServices.get(0);
            String recognizerComponent =
                new ComponentName(resolveInfo.serviceInfo.packageName,
                        resolveInfo.serviceInfo.name).flattenToShortString();
 
            mAvailableRecognizersMap.put(recognizerComponent, resolveInfo);
 
            String currentSetting = Settings.Secure.getString(
                    mFragment.getContentResolver(), Settings.Secure.VOICE_RECOGNITION_SERVICE);
            updateSettingsLink(currentSetting);
        } else { 
            // Multiple recognizers available, so show the full list of choices. 
            populateRecognizerPreference(availableRecognitionServices);
        } 
 
        // In this case, there was at least one available recognizer so 
        // we populated the settings. 
        return true; 
    } 
 
    private boolean populateOrRemoveTtsPrefs() { 
        if (mTtsEngines.getEngines().isEmpty()) {
            mVoiceCategory.removePreference(mTtsSettingsPref);
            return false; 
        } 
 
        return true; 
    }