From a76945deef38e2d27a6818991ffe802553438770 Mon Sep 17 00:00:00 2001 From: Rimas Trumpa Date: Wed, 9 Nov 2011 03:17:26 +0000 Subject: [PATCH] Added autocomplete to 'Default Activity Type' field. --- MyTracks/res/xml/preferences.xml | 2 +- .../mytracks/AutoCompleteTextPreference.java | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 MyTracks/src/com/google/android/apps/mytracks/AutoCompleteTextPreference.java diff --git a/MyTracks/res/xml/preferences.xml b/MyTracks/res/xml/preferences.xml index 25363a466..5c371eec4 100644 --- a/MyTracks/res/xml/preferences.xml +++ b/MyTracks/res/xml/preferences.xml @@ -152,7 +152,7 @@ limitations under the License. android:summaryOff="@string/settings_timestamp_track_name_summary_off" android:defaultValue="true" /> - + * This preference will store a string into the SharedPreferences. + * + * @author Rimas Trumpa (with Matt Levan) + */ +public class AutoCompleteTextPreference extends EditTextPreference { + + private static AutoCompleteTextView mEditText = null; + + public AutoCompleteTextPreference(Context context, AttributeSet attrs) { + super(context, attrs); + mEditText = new AutoCompleteTextView(context, attrs); + mEditText.setThreshold(0); + + // Gets autocomplete values for 'Default Activity' preference + if (getKey().equals(context.getString(R.string.default_category_key))) { + ArrayAdapter adapter = ArrayAdapter.createFromResource(context, + R.array.activity_types, android.R.layout.simple_dropdown_item_1line); + mEditText.setAdapter(adapter); + } + } + + @Override + protected void onBindDialogView(View view) { + AutoCompleteTextView editText = mEditText; + editText.setText(getText()); + + ViewParent oldParent = editText.getParent(); + if (oldParent != view) { + if (oldParent != null) { + ((ViewGroup) oldParent).removeView(editText); + } + onAddEditTextToDialogView(view, editText); + } + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + if (positiveResult) { + String value = mEditText.getText().toString(); + if (callChangeListener(value)) { + setText(value); + } + } + } +}