Code example for PreferenceCategory

Methods: setTitle, PreferenceGroup.addPreference, addPreference

0
		setPreferenceScreen(root);
 
		PreferenceCategory generalPreferences = new PreferenceCategory(this);
		generalPreferences.setTitle("General");
		root.addPreference(generalPreferences);
		PreferenceCategory notificationPreferences = new PreferenceCategory(this);
		notificationPreferences.setTitle("Notifications");
		root.addPreference(notificationPreferences);
 
 
		EditTextPreference upcomingRange = new EditTextPreference(this);
		upcomingRange.getEditText().setKeyListener(new NumberKeyListener() {
			 
			protected char[] getAcceptedChars() { 
				char[] numberChars = {'1','2','3','4','5','6','7','8','9','0'};
				return numberChars;
			} 
 
			 
			public int getInputType() { 
				return InputType.TYPE_NUMBER_FLAG_DECIMAL;
			} 
		}); 
		upcomingRange.getEditText().setMaxLines(1);
		upcomingRange.setKey("upcoming_range");
		upcomingRange.setTitle("Upcoming Range");
		upcomingRange.setDialogTitle("Upcoming Assignment Range In Days (0 = Only if due today, 2 = due tomorrow, etc.)");
		upcomingRange.setDefaultValue("2");
		upcomingRange.setSummary(formatSummary("upcoming_range", upcomingRange.getText()));
		generalPreferences.addPreference(upcomingRange);
 
		CheckBoxPreference notifyEnabled = new CheckBoxPreference(this);
		notifyEnabled.setKey("notifyenabled");
		notifyEnabled.setTitle("Notifications");
		notifyEnabled.setSummaryOn("Will create notifications");
		notifyEnabled.setSummaryOff("Will not create notifications");
		notifyEnabled.setDefaultValue(false);
		notificationPreferences.addPreference(notifyEnabled);
 
		ListPreference notifyMode = new ListPreference(this);
		notifyMode.setEntryValues(new String[]{"settime","interval"}); //FIXME: Remove debug setting
		notifyMode.setEntries(new String[]{"Daily (Set Time)","Set Interval"});
		notifyMode.setKey("notifymode");
		notifyMode.setTitle("Notification Mode");
		notifyMode.setDialogTitle("Notification Mode"); 
		notifyMode.setDefaultValue("settime");
		notificationPreferences.addPreference(notifyMode);  
		notifyMode.setSummary(notifyMode.getEntry());
		notifyMode.setDependency(notifyEnabled.getKey());
 
		String mode = notifyMode.getValue().toString();
		boolean isSetTime = (mode.contentEquals("settime"));
		boolean isInterval = (mode.contentEquals("interval"));
 
		TimePickerPreference notifyTime = new TimePickerPreference(this);
		notifyTime.setKey("notifytime");
		notifyTime.setTitle("Notification Time");
		notifyTime.setDialogTitle("Notification Time");
		notifyTime.setDefaultValue("8:00");
		notificationPreferences.addPreference(notifyTime);
		notifyTime.setDependency(notifyEnabled.getKey());
		notifyTime.setEnabled(isSetTime);
		notifyTime.setSummary(formatSummary("notifytime",notifyTime.getTime()));
 
		EditTextPreference notifyInterval = new EditTextPreference(this);
		notifyInterval.setKey("notifyinterval");
		notifyInterval.setTitle("Notification Interval");
		notifyInterval.setDialogTitle("Notification Interval (in hours)");
		notifyInterval.setDefaultValue("24");
		notifyInterval.getEditText().setKeyListener(new NumberKeyListener() {
			@Override 
			protected char[] getAcceptedChars() { 
				char[] numberChars = {'1','2','3','4','5','6','7','8','9','0'};
				return numberChars;
			} 
 
			@Override 
			public int getInputType() { 
				return InputType.TYPE_NUMBER_FLAG_DECIMAL;
			} 
		}); 
		notifyInterval.getEditText().setMaxLines(1);
		notificationPreferences.addPreference(notifyInterval);
		notifyInterval.setSummary(formatSummary("notifyinterval", "----"));
		notifyInterval.setEnabled(isInterval);
		notifyInterval.setDependency(notifyEnabled.getKey());
 
		TimePickerPreference notifyIntervalTime = new TimePickerPreference(this);
		notifyIntervalTime.setKey("notifyintervaltime");
		notifyIntervalTime.setTitle("Interval Start Time");
		notifyIntervalTime.setDialogTitle("Interval Start Time");
		notifyIntervalTime.setDefaultValue("8:00");
		notificationPreferences.addPreference(notifyIntervalTime);
		notifyIntervalTime.setDependency(notifyEnabled.getKey());
		notifyIntervalTime.setEnabled(isInterval);
		notifyIntervalTime.setSummary(formatSummary("notifyintervaltime",notifyIntervalTime.getTime()));
 
		return root;