PreferenceHelpers - shiraji/androidannotations GitHub Wiki
Since AndroidAnnotations 3.3
@PreferenceScreen
The @PreferenceScreen
annotation adds the preference layout to the component. This annotation only can be used on subclasses of PreferenceActivity
or PreferenceFragment
, and the annotated must have @EActivity
or @EFragment
according to the type.
The annotation value should be an XML resource id referencing the preference layout.
Usage example:
@PreferenceScreen(R.xml.settings)
@EActivity
public class SettingsActivity extends PreferenceActivity {
@PreferenceByKey(R.string.myPref1)
Preference myPreference1;
@PreferenceByKey(R.string.checkBoxPref)
CheckBoxPreference checkBoxPref;
@AfterPreferences
void initPrefs() {
checkBoxPref.setChecked(false);
}
}
Please note if you are also using @ViewById
and @AfterViews
, you may have to guard for null
. Refer to this thread for more information.
Since AndroidAnnotations 3.3.1
@PreferenceScreen
can be also used on android.support.v4.PreferenceFragment
and com.github.machinarius.preferencefragment.PreferenceFragment
. Please note these are not official Android classes, but unfortunately PreferenceFragment
is not yet available in the official Support Library.
@PreferenceByKey
This annotation can be used to inject a Preference
or Preference
subclass object to the annotated field. This annotation only can be used in subclasses of PreferenceActivity
or PreferenceFragment
, and the annotated must have @EActivity
or @EFragment
according to the type.
The annotation value must be a string resource id, referencing the key of the preference.
The injected preferences will be first available in @AfterPreferences
annotated methods.
Usage example:
@EActivity
public class SettingsActivity extends PreferenceActivity {
@PreferenceByKey(R.string.myPref1)
Preference myPreference1;
@PreferenceByKey(R.string.checkBoxPref)
CheckBoxPreference checkBoxPref;
@AfterPreferences
void initPrefs() {
checkBoxPref.setChecked(false);
}
}
@PreferenceChange
The @PreferenceChange
annotation indicates that the annotated method will be called when the corresponding Preference
is about to be changed, and will receive events defined by OnPreferenceChangeListener.onPreferenceChange
.
The annotation value should be one or several R.string.* fields that refers to Preference
or subclasses of Preference
. If not set, the method name will be used as the R.string.* field name.
The method MAY have multiple parameters:
- A
Preference
parameter to know which preference was targeted by this event - An
Object
, orSet
ofString
s, orString
to obtain the new value of thePreference
Usage example:
@PreferenceChange(R.string.myPref)
void checkedChangedOnMyButton(boolean newValue, Preference preference) {
// Something Here
}
@PreferenceChange
void myPrefPreferenceChanged(Preference preference) {
// Something Here
}
@PreferenceChange({R.string.myPref1, R.string.myPref2})
void preferenceChangeOnMultiplePrefs(Preference preference, String newValue) {
// Something Here
}
@PreferenceChange(R.string.myPref)
void preferenceChangeOnMyPref() {
// Something Here
}
Since AndroidAnnotations 3.3.1
Float
, Integer
, Long
and corresponding primitive types are now supported for the newValue
parameter. Because Android Preference
classes use String
s instead of Number
s, AndroidAnnotations automatically parses it and pass the Number
object to the @PreferenceChange
annotated method.
Usage example:
@PreferenceChange(R.string.myPref1)
void preferenceChangeIntParameter(Preference preference, int newValue) {
// Something Here
}
Since AndroidAnnotations 4.0
As of AndroidAnnotations 4.0 any subclass of Preference
can be passed to the methods (eg. ListPreference
).
@PreferenceClick
The @PreferenceClick
annotation indicates that the annotated method will be called when the corresponding Preference
is clicked by the user, and will receive events defined by OnPreferenceClickListener#onPreferenceClick
.
The annotation value should be one or several R.string.* fields that refers to Preference
or subclasses of Preference
. If not set, the method name will be used as the R.string.* field name.
The method MAY have one parameter:
- A
Preference
parameter to know which preference was clicked
Usage example:
@PreferenceClick(R.string.myPref)
void clickOnMyPref() {
// Something Here
}
@PreferenceClick
void myPrefPreferenceClicked(Preference preference) {
// Something Here
}
Since AndroidAnnotations 4.0
As of AndroidAnnotations 4.0 any subclass of Preference
can be passed to the methods (eg. ListPreference
).
@PreferenceHeaders
@PreferenceHeaders
can be used on an @EActivity
annotated subclass of PreferenceActivity
, to inject the preference headers from resource.
The annotation value should be one of R.xml.* fields.
Usage example:
@PreferenceHeaders(R.xml.preference_headers)
@EActivity
public class SettingsActivity extends PreferenceActivity {
@PreferenceByKey(R.string.myPref1)
Preference myPreference1;
@PreferenceByKey(R.string.checkBoxPref)
CheckBoxPreference checkBoxPref;
@AfterPreferences
void initPrefs() {
checkBoxPref.setChecked(false);
}
}
@AfterPreferences
Methods annotated with @AfterPreferences
will be called after addPreferenceFromResource
is called by the generated classs.
This occurs AFTER addPreferenceFromResource
which is called at the end of super.onCreate()
. Any preference depending code should be done in an @AfterPreferences
annotated method.
Usage example:
@EActivity
public class SettingsActivity extends PreferenceActivity {
@PreferenceByKey(R.string.checkBoxPref)
CheckBoxPreference checkBoxPref;
@AfterPreferences
void initPrefs() {
checkBoxPref.setChecked(false);
}
}