I have defined the layout of my settings activity like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory
android:title="Second Category"
android:key="second_category">
<EditTextPreference
android:id="@+id/pcnSetting"
android:key="pcn"
android:title="Set Canton Digits"
android:summary="Define your canton digits"
android:dialogTitle="Set Canton Digits"
android:dialogMessage="Please add your canton digits"
android:inputType="number"
android:imeOptions="actionDone"
android:maxLength="4"/>
</PreferenceCategory>
I want to import the EditTextPreference to my java code, just to be sure that the user typed in at least and maximum 4 digits.
This is how I try to get the EditTextPreference into java
canton = (EditTextPreference)findViewById(R.id.pcnSetting);
But Eclipse gives me this error:
Cannot cast from View to EditTextPreference
Why does this happend? And how can I check if the user typed in at least and maximum 4 int's?
EditTextPreference is not a View, it is a preference. So you have to use findPreference() in your PreferenceActivity. You can then do EditTextPreference.getEdittext to get the view from this preference