I have a PreferenceScreen, where the user can check/uncheck a checkbox.
To get access any Control in the Activity I used always:
var button = FindViewById<Button>(Resource.Id.myButton);
Well now, I want to access my CheckBoxPreference
..
, but its not working and I get this Exception:
The type 'Android.Preferences.CheckBoxPreference' cannot be used as type parameter 'T' in the generic type or method 'Android.App.Activity.FindViewById(int)'. There is no implicit reference conversion from 'Android.Preferences.CheckBoxPreference' to 'Android.Views.View'.
How my PreferenceScreen.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Basic"
android:id="@+id/preferenceCategory">
<CheckBoxPreference
android:id="@+id/preferenceCheckBox"
android:key="usetestdata"
android:title="Testdata ON/OFF"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
and the not working code:
var aCheckBox = FindViewById<CheckBoxPreference>(Resource.Id.preferenceCheckBox);
aCheckBox.Enabled = false;
Maybe someone can help me and tell me on how to access controls within an activity from a PreferenceScreen?
Here is the code for using PreferenceActivity
public class SettingsActivity extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//add the prefernces.xml layout
addPreferencesFromResource(R.xml.preferences);
final CheckBoxPreference cf = (CheckBoxPreference) findPreference("checkbox"));
cf.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
// do something here
return false;
}
});
}
your preferences.xml in xml folder
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:key="checkbox"
android:title="Title"/>
</PreferenceScreen>