List of usage examples for android.widget AutoCompleteTextView setThreshold
public void setThreshold(int threshold)
Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
When threshold
is less than or equals 0, a threshold of 1 is applied.
From source file:com.dish.browser.activity.BrowserActivity.java
/** * method to generate search suggestions for the AutoCompleteTextView from * previously searched URLs//from w w w.j a v a 2 s. co m */ private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) { getUrl.setThreshold(1); getUrl.setDropDownWidth(-1); getUrl.setDropDownAnchor(R.id.toolbar_layout); getUrl.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { try { String url; url = ((TextView) arg1.findViewById(R.id.url)).getText().toString(); if (url.startsWith(mActivity.getString(R.string.suggestion))) { url = ((TextView) arg1.findViewById(R.id.title)).getText().toString(); } else { getUrl.setText(url); } searchTheWeb(url); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0); if (mCurrentView != null) { mCurrentView.requestFocus(); } } catch (NullPointerException e) { Log.e("Browser Error: ", "NullPointerException on item click"); } } }); getUrl.setSelectAllOnFocus(true); mSearchAdapter = new SearchAdapter(mActivity, mDarkTheme, isIncognito()); getUrl.setAdapter(mSearchAdapter); }
From source file:pro.dbro.bart.TheActivity.java
/** Called when the activity is first created. */ @Override/* w w w .j a v a 2 s. com*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //TESTING: enable crittercism Crittercism.init(getApplicationContext(), SECRETS.CRITTERCISM_SECRET); if (Build.VERSION.SDK_INT < 11) { //If API 14+, The ActionBar will be hidden with this call this.requestWindowFeature(Window.FEATURE_NO_TITLE); } setContentView(R.layout.main); tableLayout = (TableLayout) findViewById(R.id.tableLayout); tableContainerLayout = (LinearLayout) findViewById(R.id.tableContainerLayout); c = this; res = getResources(); prefs = getSharedPreferences("PREFS", 0); editor = prefs.edit(); if (prefs.getBoolean("first_timer", true)) { TextView greetingTv = (TextView) View.inflate(c, R.layout.tabletext, null); greetingTv.setText(Html.fromHtml(getString(R.string.greeting))); greetingTv.setTextSize(18); greetingTv.setPadding(0, 0, 0, 0); greetingTv.setMovementMethod(LinkMovementMethod.getInstance()); new AlertDialog.Builder(c).setTitle("Welcome to Open BART").setIcon(R.drawable.ic_launcher) .setView(greetingTv).setPositiveButton("Okay!", null).show(); editor.putBoolean("first_timer", false); editor.commit(); } // LocalBroadCast Stuff LocalBroadcastManager.getInstance(this).registerReceiver(serviceStateMessageReceiver, new IntentFilter("service_status_change")); // infoLayout is at the bottom of the screen // currently contains the stop service label infoLayout = (LinearLayout) findViewById(R.id.infoLayout); // Assign the stationSuggestions Set stationSuggestions = new ArrayList(); // Assign the bart station list to the autocompletetextviews ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, BART.STATIONS); originTextView = (AutoCompleteTextView) findViewById(R.id.originTv); // Set tag for array adapter switch originTextView.setTag(R.id.TextInputShowingSuggestions, "false"); fareTv = (TextView) findViewById(R.id.fareTv); stopServiceTv = (TextView) findViewById(R.id.stopServiceTv); destinationTextView = (AutoCompleteTextView) findViewById(R.id.destinationTv); destinationTextView.setTag(R.id.TextInputShowingSuggestions, "false"); destinationTextView.setAdapter(adapter); originTextView.setAdapter(adapter); // Retrieve TextView inputs from saved preferences if (prefs.contains("origin") && prefs.contains("destination")) { //state= originTextView,destinationTextView String origin = prefs.getString("origin", ""); String destination = prefs.getString("destination", ""); if (origin.compareTo("") != 0) originTextView.setThreshold(200); // disable auto-complete until new text entered if (destination.compareTo("") != 0) destinationTextView.setThreshold(200); // disable auto-complete until new text entered originTextView.setText(origin); destinationTextView.setText(destination); validateInputAndDoRequest(); } // Retrieve station suggestions from file storage try { ArrayList<StationSuggestion> storedSuggestions = (ArrayList<StationSuggestion>) LocalPersistence .readObjectFromFile(c, res.getResourceEntryName(R.string.StationSuggestionFileName)); // If stored StationSuggestions are found, apply them if (storedSuggestions != null) { stationSuggestions = storedSuggestions; Log.d("stationSuggestions", "Loaded"); } else Log.d("stationSuggestions", "Not Found"); } catch (Throwable t) { // don't sweat it } ImageButton map = (ImageButton) findViewById(R.id.map); map.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(c, MapActivity.class); startActivity(intent); } }); ImageButton reverse = (ImageButton) findViewById(R.id.reverse); reverse.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Editable originTempText = originTextView.getText(); originTextView.setText(destinationTextView.getText()); destinationTextView.setText(originTempText); validateInputAndDoRequest(); } }); // Handles restoring TextView input when focus lost, if no new input entered // previous input is stored in the target View Tag attribute // Assumes the target view is a TextView // TODO:This works but starts autocomplete when the view loses focus after clicking outside the autocomplete listview OnFocusChangeListener inputOnFocusChangeListener = new OnFocusChangeListener() { @Override public void onFocusChange(View inputTextView, boolean hasFocus) { if (inputTextView.getTag(R.id.TextInputMemory) != null && !hasFocus && ((TextView) inputTextView).getText().toString().compareTo("") == 0) { //Log.v("InputTextViewTagGet","orig: "+ inputTextView.getTag()); ((TextView) inputTextView).setText(inputTextView.getTag(R.id.TextInputMemory).toString()); } } }; originTextView.setOnFocusChangeListener(inputOnFocusChangeListener); destinationTextView.setOnFocusChangeListener(inputOnFocusChangeListener); // When the TextView is clicked, store current text in TextView's Tag property, clear displayed text // and enable Auto-Completing after first character entered OnTouchListener inputOnTouchListener = new OnTouchListener() { @Override public boolean onTouch(View inputTextView, MotionEvent me) { // Only perform this logic on finger-down if (me.getAction() == me.ACTION_DOWN) { inputTextView.setTag(R.id.TextInputMemory, ((TextView) inputTextView).getText().toString()); Log.d("adapterSwitch", "suggestions"); ((AutoCompleteTextView) inputTextView).setThreshold(1); ((TextView) inputTextView).setText(""); // TESTING // set tag to be retrieved on input entered to set adapter back to station list // The key of a tag must be a unique ID resource inputTextView.setTag(R.id.TextInputShowingSuggestions, "true"); ArrayList<StationSuggestion> prunedSuggestions = new ArrayList<StationSuggestion>(); // copy suggestions for (int x = 0; x < stationSuggestions.size(); x++) { prunedSuggestions.add(stationSuggestions.get(x)); } // Check for and remove other text input's value from stationSuggestions if (inputTextView.equals(findViewById(R.id.originTv))) { // If the originTv is clicked, remove the destinationTv's value from prunedSuggestions if (prunedSuggestions.contains(new StationSuggestion( ((TextView) findViewById(R.id.destinationTv)).getText().toString(), "recent"))) { prunedSuggestions.remove(new StationSuggestion( ((TextView) findViewById(R.id.destinationTv)).getText().toString(), "recent")); } } else if (inputTextView.equals(findViewById(R.id.destinationTv))) { // If the originTv is clicked, remove the destinationTv's value from prunedSuggestions if (prunedSuggestions.contains(new StationSuggestion( ((TextView) findViewById(R.id.originTv)).getText().toString(), "recent"))) { prunedSuggestions.remove(new StationSuggestion( ((TextView) findViewById(R.id.originTv)).getText().toString(), "recent")); } } //if(stationSuggestions.contains(new StationSuggestion(((TextView)inputTextView).getText().toString(),"recent"))) // if available, add localStation to prunedSuggestions if (localStation.compareTo("") != 0) { if (BART.REVERSE_STATION_MAP.get(localStation) != null) { // If a valid localStation (based on DeviceLocation) is available: // remove localStations from recent suggestions (if it exists there) // and add as nearby station prunedSuggestions.remove( new StationSuggestion(BART.REVERSE_STATION_MAP.get(localStation), "recent")); prunedSuggestions.add( new StationSuggestion(BART.REVERSE_STATION_MAP.get(localStation), "nearby")); } } // TESTING: Set Custom ArrayAdapter to hold recent/nearby stations TextPlusIconArrayAdapter adapter = new TextPlusIconArrayAdapter(c, prunedSuggestions); ((AutoCompleteTextView) inputTextView).setAdapter(adapter); // force drop-down to appear, overriding requirement that at least one char is entered ((AutoCompleteTextView) inputTextView).showDropDown(); // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, // android.R.layout.simple_dropdown_item_1line, BART.STATIONS); } // Allow Android to handle additional actions - i.e: TextView takes focus return false; } }; originTextView.setOnTouchListener(inputOnTouchListener); destinationTextView.setOnTouchListener(inputOnTouchListener); // Autocomplete ListView item select listener originTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) { Log.d("OriginTextView", "item clicked"); AutoCompleteTextView originTextView = (AutoCompleteTextView) findViewById(R.id.originTv); originTextView.setThreshold(200); //hideSoftKeyboard(arg1); // calling hideSoftKeyboard with arg1 doesn't work with stationSuggestion adapter hideSoftKeyboard(findViewById(R.id.inputLinearLayout)); // Add selected station to stationSuggestions ArrayList if it doesn't exist if (!stationSuggestions .contains((new StationSuggestion(originTextView.getText().toString(), "recent")))) { stationSuggestions.add(0, new StationSuggestion(originTextView.getText().toString(), "recent")); // if the stationSuggestion arraylist is over the max size, remove the last item if (stationSuggestions.size() > STATION_SUGGESTION_SIZE) { stationSuggestions.remove(stationSuggestions.size() - 1); } } // Else, increment click count for that recent else { stationSuggestions .get(stationSuggestions.indexOf( (new StationSuggestion(originTextView.getText().toString(), "recent")))) .addHit(); } validateInputAndDoRequest(); } }); destinationTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) { Log.d("DestinationTextView", "item clicked"); // Actv not available as arg1 AutoCompleteTextView destinationTextView = (AutoCompleteTextView) findViewById(R.id.destinationTv); destinationTextView.setThreshold(200); //hideSoftKeyboard(arg1); hideSoftKeyboard(findViewById(R.id.inputLinearLayout)); // Add selected station to stationSuggestions set if (!stationSuggestions .contains((new StationSuggestion(destinationTextView.getText().toString(), "recent")))) { Log.d("DestinationTextView", "adding station"); stationSuggestions.add(0, new StationSuggestion(destinationTextView.getText().toString(), "recent")); if (stationSuggestions.size() > STATION_SUGGESTION_SIZE) { stationSuggestions.remove(stationSuggestions.size() - 1); } } // If station exists in StationSuggestions, increment hit else { stationSuggestions .get(stationSuggestions.indexOf( (new StationSuggestion(destinationTextView.getText().toString(), "recent")))) .addHit(); //Log.d("DestinationTextView",String.valueOf(stationSuggestions.get(stationSuggestions.indexOf((new StationSuggestion(destinationTextView.getText().toString(),"recent")))).hits)); } //If a valid origin station is not entered, return if (BART.STATION_MAP.get(originTextView.getText().toString()) == null) return; validateInputAndDoRequest(); //lastRequest = "etd"; //String url = "http://api.bart.gov/api/etd.aspx?cmd=etd&orig="+originStation+"&key=MW9S-E7SL-26DU-VV8V"; // TEMP: For testing route function //lastRequest = "route"; //bartApiRequest(); } }); //OnKeyListener only gets physical device keyboard events (except the softkeyboard delete key. hmmm) originTextView.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { //Log.d("seachScreen", "afterTextChanged"); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { //Log.d("seachScreen", "beforeTextChanged"); } public void onTextChanged(CharSequence s, int start, int before, int count) { ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, android.R.layout.simple_dropdown_item_1line, BART.STATIONS); if (((String) ((TextView) findViewById(R.id.originTv)).getTag(R.id.TextInputShowingSuggestions)) .compareTo("true") == 0) { ((TextView) findViewById(R.id.originTv)).setTag(R.id.TextInputShowingSuggestions, "false"); ((AutoCompleteTextView) findViewById(R.id.originTv)).setAdapter(adapter); } Log.d("seachScreen", s.toString()); } }); destinationTextView.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { //Log.d("seachScreen", "afterTextChanged"); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { //Log.d("seachScreen", "beforeTextChanged"); } public void onTextChanged(CharSequence s, int start, int before, int count) { ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, android.R.layout.simple_dropdown_item_1line, BART.STATIONS); if (((String) ((TextView) findViewById(R.id.destinationTv)) .getTag(R.id.TextInputShowingSuggestions)).compareTo("true") == 0) { ((TextView) findViewById(R.id.destinationTv)).setTag(R.id.TextInputShowingSuggestions, "false"); ((AutoCompleteTextView) findViewById(R.id.destinationTv)).setAdapter(adapter); } Log.d("seachScreen", s.toString()); } }); }