Search code examples
androidpreferences

android EditText in preference screen


Can I put an EditText element in a preference screen?

I want to do this to allow fast searching of a large ListPreference by typing, but do not want to save the contents of the search box? Is this possible, or are standard layout controls not allowed on preference screens.

The goal is to provide a means of selecting a time-zone and saving it as a preference. Using a ListViewPrefernce alone isn't sufficient because of the huge amount of time-zones that must be scrolled though. Hence, I would like to provide a "hypersearch" type of input box where the user can type a time-zone or part of one to narrow the list box selection down.


Solution

  • What you could do is just make a standard Preference which will show a dialog containing an EditText, and add an ontextchangelistener:

    searchET.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub              
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub              
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });
    

    and as far as showing the search results, I don't know. I've never tried any of this specifically before, but it seems like it would work for you, and you'd have to figure out whatever code is needed to perform the search itself, but this would add the initial functionality.

    EDIT: By standard Preference, I mean in the XML:

    <Preference
        android:title="Time Zone"
        android:summary="Choose your time zone"
        android:key="timeZone"/>
    

    And in your PreferenceActivity add the following:

    Preference timeZone = (Preference)findPreference("timeZone");
         timeZone.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    
                @Override
                public boolean onPreferenceClick(Preference arg0) {
                    showDialog(1);
                    return false;
                }
            });
    
    
    
    @Override
        protected Dialog onCreateDialog(int id) {
    
        switch (id) {       
    
        case 1: 
            final EditText searchET = new EditText(this);
    //do the searchET.addTextChangedListener here
                     return new AlertDialog.Builder(this)
    .setTitle("Choose Time Zone")
    .setView(showAppString)
    .setPositiveButton(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
        //save the time zone to sharedPreferences
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreference(this);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("TIME_ZONE", TZ.toString());
    
    }//closes the onClick
    })//closes the onclicklistener
    .show();
    }//closes the switch
    
    }//closes the oncreatedialog
    

    Though, I think you could actually just use an EditTextPreference and then addTextChangedListner to the EditTextPreference, since I THINK it inherits everything from EditTexts. I'm not postitive, though.