0

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?

eMi
  • 5,540
  • 10
  • 60
  • 109

1 Answers1

1

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>
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • thanks, that worked, I could access my CheckBoxPreference.. Do you also know, how I could now set CheckBoxPreference to IsEnabled = false? There's no Property Enabled, like the normal CheckBox has.. any ideas? – eMi Nov 19 '12 at 13:35
  • using this method you can set value cf.setValue(true); – Pratik Nov 19 '12 at 13:54
  • I don't meant Checked/Unchecked.. I fixed the problem now anyway, I meant this: cf.Enabled = false; – eMi Nov 19 '12 at 13:56