├── AutoCompleteEditText.java ├── LICENSE ├── README.md └── preview.gif /AutoCompleteEditText.java: -------------------------------------------------------------------------------- 1 | package im.dacer; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.WindowManager; 6 | import android.widget.ArrayAdapter; 7 | import android.widget.AutoCompleteTextView; 8 | 9 | import java.util.ArrayList; 10 | import java.util.regex.Matcher; 11 | import java.util.regex.Pattern; 12 | 13 | 14 | /** 15 | * Created by Dacer on 5/23/14. 16 | * Show a drop-down list after input a symbol. 17 | */ 18 | public class AutoCompleteEditText extends AutoCompleteTextView { 19 | 20 | private ArrayAdapter adapter; 21 | private String startAtSymbol = "#"; 22 | 23 | public AutoCompleteEditText(Context context){ 24 | this(context, null); 25 | } 26 | public AutoCompleteEditText(Context context, AttributeSet attrs){ 27 | super(context,attrs); 28 | init(); 29 | } 30 | public AutoCompleteEditText(Context context, AttributeSet attrs, int defStyle){ 31 | super(context, attrs, defStyle); 32 | init(); 33 | } 34 | 35 | private void init(){ 36 | adapter = new ArrayAdapter(getContext(), 37 | android.R.layout.simple_dropdown_item_1line, new ArrayList()); 38 | setAdapter(adapter); 39 | setDropDownWidth(WindowManager.LayoutParams.WRAP_CONTENT); 40 | } 41 | 42 | /** 43 | * 44 | * @param symbol default is # 45 | */ 46 | public void setStartAtSymbol(String symbol){ 47 | startAtSymbol = symbol; 48 | } 49 | 50 | /** 51 | * 52 | * @param dataList The data must start with the symbol 53 | */ 54 | public void setAutoCompleteList(String[] dataList){ 55 | adapter = new ArrayAdapter(getContext(), 56 | android.R.layout.simple_dropdown_item_1line, dataList); 57 | setAdapter(adapter); 58 | } 59 | 60 | @Override 61 | public boolean enoughToFilter() { 62 | if(getText() != null){ 63 | return getText().length() != 0; 64 | } 65 | return true; 66 | } 67 | 68 | @Override 69 | protected void performFiltering(CharSequence text, int keyCode){ 70 | String beforeCursor = getText().toString().substring(0, getSelectionStart()); 71 | Pattern pattern = Pattern.compile(getRegularExpression()); 72 | Matcher matcher = pattern.matcher(beforeCursor); 73 | if (matcher.find()) { 74 | text = matcher.group(0);; 75 | } 76 | super.performFiltering(text, keyCode); 77 | } 78 | 79 | @Override 80 | protected void replaceText(CharSequence text){ 81 | String beforeCursor = getText().toString().substring(0, getSelectionStart()); 82 | String afterCursor = getText().toString().substring(getSelectionStart()); 83 | 84 | Pattern pattern = Pattern.compile("#\\S*"); 85 | Matcher matcher = pattern.matcher(beforeCursor); 86 | StringBuffer sb = new StringBuffer(); 87 | int matcherStart = 0; 88 | while (matcher.find()) { 89 | int curPos = getSelectionStart(); 90 | if(curPos > matcher.start() && 91 | curPos <= matcher.end()){ 92 | matcherStart = matcher.start(); 93 | matcher.appendReplacement(sb, text.toString()+" "); 94 | } 95 | } 96 | matcher.appendTail(sb); 97 | setText(sb.toString()+afterCursor); 98 | setSelection(matcherStart + text.length()+1); 99 | } 100 | 101 | 102 | private String getRegularExpression(){ 103 | return startAtSymbol+"\\S*\\z"; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Ding Wenhao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoCompleteEditText 2 | 3 | A simple EditText that can show a drop-down list after input a symbol. 4 | 5 | ![AutoCompleteEditText](https://raw.githubusercontent.com/HackPlan/Android-AutoCompleteEditText/master/preview.gif) 6 | 7 | It was used in [Pomotodo](https://play.google.com/store/apps/details?id=com.pomotodo) 8 | 9 | ## Usage 10 | 11 | ```xml 12 | 16 | ``` 17 | 18 | ```java 19 | AutoCompleteEditText editText = (AutoCompleteEditText) findViewById(R.id.edit_text); 20 | editText.setStartAtSymbol("#"); 21 | editText.setAutoCompleteList(list); 22 | ``` 23 | 24 | ## License 25 | 26 | The MIT License (MIT) 27 | 28 | Copyright (c) 2014 Ding Wenhao 29 | 30 | Permission is hereby granted, free of charge, to any person obtaining a copy of 31 | this software and associated documentation files (the "Software"), to deal in 32 | the Software without restriction, including without limitation the rights to 33 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 34 | the Software, and to permit persons to whom the Software is furnished to do so, 35 | subject to the following conditions: 36 | 37 | The above copyright notice and this permission notice shall be included in all 38 | copies or substantial portions of the Software. 39 | 40 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 41 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 42 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 43 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 44 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 45 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 46 | 47 | 48 | ## Contributing 49 | 50 | Please fork this repository and contribute back using 51 | [pull requests](https://github.com/github/android/pulls). 52 | 53 | Any contributions, large or small, major features, bug fixes, additional 54 | language translations, unit/integration tests are welcomed -------------------------------------------------------------------------------- /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackPlan/Android-AutoCompleteEditText/cc21f47600550266360fd88718bb53b96f121505/preview.gif --------------------------------------------------------------------------------