How to get EditTextPreference reference in main activity android

900 views Asked by At

How can I get a reference to my EditTextPreference in my main activity?

I have tried this:

EditTextPreference mEditTextPref =(EditTextPreference) findPreference(incrementby);

but its giving me this error;

incrementby cannot be resolved to a variable

This is the code for the EditTextPreference

        android:defaultValue="1"
        android:inputType="number"
        android:key="incrementby"
        android:maxLength="3"
        android:summary="Set the incremental value of the counter"
        android:title="Increase Count by" />

After getting a reference to the EditTextPreferece, what I want to do is, use this reference to set the value of the EditTextPreferece through my main activity. Something like;

mEditTextPref.setText("1");
1

There are 1 answers

6
Blue_Alien On

the error says it: it is not a variable!! that is, the 'icrementby' is not a variable and expected a string! so enclose it in a double quotes ("incrementby").

EditTextPreference mEditTextPref =(EditTextPreference) findPreference("incrementby");

EDIT

create a local variable in your activity:

Context mContext; 

and in the onCreate, do:

mContext=getBaseContext();

then use

EditTextPreference mEditTextPref =(EditTextPreference) mContext.findPreference("incrementby");