Java Code Examples for android.widget.AutoCompleteTextView#setOnItemClickListener()

The following examples show how to use android.widget.AutoCompleteTextView#setOnItemClickListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrowserActivity.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
/**
 * method to generate search suggestions for the AutoCompleteTextView from
 * previously searched URLs
 */
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) {

		}

	});

	getUrl.setSelectAllOnFocus(true);
	mSearchAdapter = new SearchAdapter(mActivity, mDarkTheme, isIncognito());
	getUrl.setAdapter(mSearchAdapter);
}
 
Example 2
Source File: SearchActivity.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView =
            (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);
    searchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
    searchView.setQuery(mPresenter.getSearchModels().get(0).getQuery(), false);
    if (isInputMode) {
        MenuItemCompat.expandActionView(searchItem);
    } else {
        MenuItemCompat.collapseActionView(searchItem);
    }
    MenuItemCompat.setOnActionExpandListener(searchItem, this);

    AutoCompleteTextView autoCompleteTextView = searchView
            .findViewById(android.support.v7.appcompat.R.id.search_src_text);
    autoCompleteTextView.setThreshold(0);
    autoCompleteTextView.setAdapter(new ArrayAdapter<>(this,
            R.layout.layout_item_simple_list, mPresenter.getSearchRecordList()));
    autoCompleteTextView.setDropDownBackgroundDrawable(new ColorDrawable(ViewUtils.getWindowBackground(getActivity())));
    autoCompleteTextView.setOnItemClickListener((parent, view, position, id) -> {
        onQueryTextSubmit(parent.getAdapter().getItem(position).toString());
    });

    return super.onCreateOptionsMenu(menu);
}
 
Example 3
Source File: BrowserActivity.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * method to generate search suggestions for the AutoCompleteTextView from
 * previously searched URLs
 */
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {

    mSuggestionsAdapter = new SuggestionsAdapter(this, mDarkTheme, isIncognito());

    getUrl.setThreshold(1);
    getUrl.setDropDownWidth(-1);
    getUrl.setDropDownAnchor(R.id.toolbar_layout);
    getUrl.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
            String url = null;
            CharSequence urlString = ((TextView) view.findViewById(R.id.url)).getText();
            if (urlString != null) {
                url = urlString.toString();
            }
            if (url == null || url.startsWith(getString(R.string.suggestion))) {
                CharSequence searchString = ((TextView) view.findViewById(R.id.title)).getText();
                if (searchString != null) {
                    url = searchString.toString();
                }
            }
            if (url == null) {
                return;
            }
            getUrl.setText(url);
            searchTheWeb(url);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0);
            mPresenter.onAutoCompleteItemPressed();
        }

    });

    getUrl.setSelectAllOnFocus(true);
    getUrl.setAdapter(mSuggestionsAdapter);
}
 
Example 4
Source File: AutoCompleteCityTextViewGenerator.java    From privacy-friendly-weather with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param editField    The component to "transform" into one that shows a city drop down list
 *                     based on the current input. Make sure to pass an initialized object,
 *                     else a java.lang.NullPointerException will be thrown.
 * @param listLimit    Determines how many items shall be shown in the drop down list at most.
 */
public void generate(AutoCompleteTextView editField, int listLimit, final int enterActionId, final MyConsumer<City> cityConsumer, final Runnable selectAction) {
    cityAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, new ArrayList<City>());
    this.editField = editField;
    this.cityConsumer = cityConsumer;
    this.listLimit = listLimit;
    editField.setAdapter(cityAdapter);
    editField.addTextChangedListener(new TextChangeListener());

    editField.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedCity = (City) parent.getItemAtPosition(position);
            cityConsumer.accept(selectedCity);
        }
    });

    editField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == enterActionId) {
                Boolean checkCity = checkCity();
                if (checkCity) {
                    selectAction.run();
                }
                return true;
            }
            return false;
        }
    });
}
 
Example 5
Source File: MainActivity.java    From android-play-places with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Construct a GeoDataClient for the Google Places API for Android.
    mGeoDataClient = Places.getGeoDataClient(this, null);

    setContentView(R.layout.activity_main);

    // Retrieve the AutoCompleteTextView that will display Place suggestions.
    mAutocompleteView = (AutoCompleteTextView)
            findViewById(R.id.autocomplete_places);

    // Register a listener that receives callbacks when a suggestion has been selected
    mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);

    // Retrieve the TextViews that will display details and attributions of the selected place.
    mPlaceDetailsText = (TextView) findViewById(R.id.place_details);
    mPlaceDetailsAttribution = (TextView) findViewById(R.id.place_attribution);

    // Set up the adapter that will retrieve suggestions from the Places Geo Data Client.
    mAdapter = new PlaceAutocompleteAdapter(this, mGeoDataClient, BOUNDS_GREATER_SYDNEY, null);
    mAutocompleteView.setAdapter(mAdapter);

    // Set up the 'clear text' button that clears the text in the autocomplete view
    Button clearButton = (Button) findViewById(R.id.button_clear);
    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAutocompleteView.setText("");
        }
    });
}
 
Example 6
Source File: BrowserActivity.java    From JumpGo with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * method to generate search suggestions for the AutoCompleteTextView from
 * previously searched URLs
 */
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {

    mSuggestionsAdapter = new SuggestionsAdapter(this, mDarkTheme, isIncognito());

    getUrl.setThreshold(1);
    getUrl.setDropDownWidth(-1);
    getUrl.setDropDownAnchor(R.id.toolbar_layout);
    getUrl.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
            String url = null;
            CharSequence urlString = ((TextView) view.findViewById(R.id.url)).getText();
            if (urlString != null) {
                url = urlString.toString();
            }
            if (url == null || url.startsWith(getString(R.string.suggestion))) {
                CharSequence searchString = ((TextView) view.findViewById(R.id.title)).getText();
                if (searchString != null) {
                    url = searchString.toString();
                }
            }
            if (url == null) {
                return;
            }
            getUrl.setText(url);
            searchTheWeb(url);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0);
            mPresenter.onAutoCompleteItemPressed();
        }

    });

    getUrl.setSelectAllOnFocus(true);
    getUrl.setAdapter(mSuggestionsAdapter);
}
 
Example 7
Source File: AutoCompleteActivity.java    From coursera-android with MIT License 5 votes vote down vote up
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	// Get a reference to the AutoCompleteTextView
	AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);

	// Create an ArrayAdapter containing country names
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			R.layout.list_item, COUNTRIES);
	
	// Set the adapter for the AutoCompleteTextView
	textView.setAdapter(adapter);
	
	textView.setOnItemClickListener(new OnItemClickListener() {

		// Display a Toast Message when the user clicks on an item in the AutoCompleteTextView
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Toast.makeText(getApplicationContext(), "The winner is:" + arg0.getAdapter().getItem(arg2), Toast.LENGTH_SHORT).show();
			
		}
	});
}
 
Example 8
Source File: AutoCompleteActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String[] mCountries = getResources().getStringArray(R.array.country_names);

    // Get a reference to the AutoCompleteTextView
    final AutoCompleteTextView textView = findViewById(R.id.autocomplete_country);

    // Create an ArrayAdapter containing country names
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            R.layout.list_item, mCountries);

    // Set the adapter for the AutoCompleteTextView
    textView.setAdapter(adapter);

    // Display a Toast Message when the user clicks on an item in the AutoCompleteTextView
    textView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                                long arg3) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.winner_is_string, arg0.getAdapter().getItem(arg2)),
                    Toast.LENGTH_LONG).show();
            textView.setText("");
        }
    });
}
 
Example 9
Source File: Main.java    From iZhihu with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
    mAutoCompleteTextView = (AutoCompleteTextView) menuItem.getActionView().findViewById(R.id.search);

    questionsAdapter = new QuestionsAdapter(this, searchedQuestions);
    questionsAdapter.setHideDescription(true); // Hide Description

    mAutoCompleteTextView.addTextChangedListener(textWatcher);
    mAutoCompleteTextView.setAdapter(questionsAdapter);
    mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Helper.startDetailActivity(Main.this, searchedQuestions, i);
        }
    });

    // Request focus.
    mAutoCompleteTextView.requestFocus();
    (new Timer()).schedule(
            new TimerTask() {
                @Override
                public void run() {
                    mInputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }, 10
    );
    return true;
}
 
Example 10
Source File: SearchPlaceOnMapActivity.java    From Airbnb-Android-Google-Map-View with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_place_on_map);

    getSupportActionBar().setTitle("Search Places");

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    fabAdd= (FloatingActionButton) findViewById(R.id.fab_add);
    fabAdd.setOnClickListener(this);

    mAutocompleteView = (AutoCompleteTextView)
            findViewById(R.id.googleplacesearch);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, 0 /* clientId */, this)
            .addApi(Places.GEO_DATA_API)
            .build();
    mAdapter = new PlaceAutocompleteAdapter(this, R.layout.google_places_search_items,
            mGoogleApiClient, null, null);

    //TODO:In order to Restrict search to India uncomment this and comment the above line
    /*
    mAdapter = new PlaceAutocompleteAdapter(this, R.layout.google_places_search_items,
            mGoogleApiClient, BOUNDS_GREATER_INDIA, null);
     */
    mAutocompleteView.setAdapter(mAdapter);
    mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);

    mAutocompleteView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;

            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (event.getRawX() >= (mAutocompleteView.getRight() - mAutocompleteView.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                    mAutocompleteView.setText("");

                    return true;
                }
            }
            if (event.getRawX() <= (mAutocompleteView.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width())) {

                Intent i = new Intent(SearchPlaceOnMapActivity.this, MainActivity.class);
                startActivity(i);

                finish();
                return true;
            }
            return false;
        }
    });
}
 
Example 11
Source File: AddItemShoppingList.java    From ShoppingList with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_add_item_shopping_list);

	try {
		shoppingList = ShoppingListDAO.select(this, getIntent().getExtras().getInt((getString(R.string.id_shopping_list))));
	} catch (VansException e) {
		Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		e.printStackTrace();
	}

	this.setTitle(shoppingList.getName());

	lvItensShoppingList = (ListView) findViewById(R.id.lvItemShoppingList);
	lvItensShoppingList.setOnItemClickListener(this);
	lvItensShoppingList.setOnItemLongClickListener(this);

	headerView = (View) getLayoutInflater().inflate(R.layout.header_list_view_item_shopping_list, null);
	lvItensShoppingList.addHeaderView(headerView, null, false);

	adapter = new ItemShoppingListCursorAdapter(this, shoppingList.getId());
	lvItensShoppingList.setAdapter(adapter);

	edUnitValue = (EditText) findViewById(R.id.edUnitValue);
	edUnitValue.setVisibility(UserPreferences.getShowUnitValue(this) ? View.VISIBLE : View.GONE);
	edUnitValue.setOnKeyListener(this);
	edUnitValue.addTextChangedListener(new CustomEditTextWatcher(edUnitValue, 5));
	edUnitValue.setOnFocusChangeListener(this);

	edQuantity = (EditText) findViewById(R.id.edQuantity);
	edQuantity.addTextChangedListener(new CustomEditTextWatcher(edQuantity, 4));
	edQuantity.setVisibility(UserPreferences.getShowQuantity(this) ? View.VISIBLE : View.GONE);
	edQuantity.setOnFocusChangeListener(this);

	edDescription = (AutoCompleteTextView) findViewById(R.id.edDescription);
	edDescription.setOnItemClickListener(this);
	edDescription.addTextChangedListener(new CustomEditTextWatcher(edDescription, -1));

	if ((!UserPreferences.getShowQuantity(this)) && (!UserPreferences.getShowUnitValue(this))) {
		edDescription.setImeOptions(EditorInfo.IME_ACTION_GO);
		edDescription.setOnKeyListener(this);
	} else if (!UserPreferences.getShowUnitValue(this)) {
		edQuantity.setImeOptions(EditorInfo.IME_ACTION_GO);
		edQuantity.setOnKeyListener(this);
	}

}
 
Example 12
Source File: MainActivity.java    From ExamplesAndroid with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage
    // functionality, which automatically sets up the API client to handle Activity lifecycle
    // events. If your activity does not extend FragmentActivity, make sure to call connect()
    // and disconnect() explicitly.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, 0 /* clientId */, this)
            .addApi(Places.GEO_DATA_API)
            .build();

    setContentView(R.layout.activity_main);

    // Retrieve the AutoCompleteTextView that will display Place suggestions.
    mAutocompleteView = (AutoCompleteTextView)
            findViewById(R.id.autocomplete_places);

    // Register a listener that receives callbacks when a suggestion has been selected
    mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);

    // Retrieve the TextViews that will display details and attributions of the selected place.
    mPlaceDetailsText = (TextView) findViewById(R.id.place_details);
    mPlaceDetailsAttribution = (TextView) findViewById(R.id.place_attribution);

    // Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover
    // the entire world.
    mAdapter = new PlaceAutocompleteAdapter(this, mGoogleApiClient, BOUNDS_GREATER_SYDNEY,
            null);
    mAutocompleteView.setAdapter(mAdapter);

    // Set up the 'clear text' button that clears the text in the autocomplete view
    Button clearButton = (Button) findViewById(R.id.button_clear);
    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAutocompleteView.setText("");
        }
    });
}
 
Example 13
Source File: DownloadManager.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
@SuppressLint("InflateParams")
public DownloadDialog(Context context, DialogCallback callback, String chanName,
		String boardName, String threadNumber, String threadTitle,
		boolean allowDetailedFileName, boolean allowOriginalName) {
	File root = Preferences.getDownloadDirectory();
	this.context = context;
	this.callback = callback;
	this.chanName = chanName;
	this.boardName = boardName;
	this.threadNumber = threadNumber;
	inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

	View view = LayoutInflater.from(context).inflate(R.layout.dialog_download_choice, null);
	RadioGroup radioGroup = view.findViewById(R.id.download_choice);
	radioGroup.check(R.id.download_common);
	radioGroup.setOnCheckedChangeListener(this);
	((RadioButton) view.findViewById(R.id.download_common)).setText(context
			.getString(R.string.text_download_to_format, root.getName()));

	detailNameCheckBox = view.findViewById(R.id.download_detail_name);
	originalNameCheckBox = view.findViewById(R.id.download_original_name);
	if (chanName == null && boardName == null && threadNumber == null) {
		allowDetailedFileName = false;
	}
	if (allowDetailedFileName) {
		detailNameCheckBox.setChecked(Preferences.isDownloadDetailName());
	} else {
		detailNameCheckBox.setVisibility(View.GONE);
	}
	if (allowOriginalName) {
		originalNameCheckBox.setChecked(Preferences.isDownloadOriginalName());
	} else {
		originalNameCheckBox.setVisibility(View.GONE);
	}

	AutoCompleteTextView editText = view.findViewById(android.R.id.text1);
	if (!allowDetailedFileName && !allowOriginalName) {
		((ViewGroup.MarginLayoutParams) editText.getLayoutParams()).topMargin = 0;
	}

	if (threadNumber != null) {
		String chanTitle = ChanConfiguration.get(chanName).getTitle();
		if (threadTitle != null) {
			threadTitle = StringUtils.escapeFile(StringUtils.cutIfLongerToLine(threadTitle, 50, false), false);
		}
		String text = Preferences.getSubdir(chanName, chanTitle, boardName, threadNumber, threadTitle);
		editText.setText(text);
		editText.setSelection(text.length());
		if (StringUtils.isEmpty(text)) {
			text = Preferences.formatSubdir(Preferences.DEFAULT_SUBDIR_PATTERN, chanName, chanTitle, boardName,
					threadNumber, threadTitle);
		}
		editText.setHint(text);
	}

	editText.setEnabled(false);
	editText.setOnEditorActionListener(this);
	editText.setOnItemClickListener(this);
	this.editText = editText;
	dropDownRunnable = this.editText::showDropDown;

	adapter = new DialogAdapter(root, () -> {
		if (DownloadDialog.this.editText.isEnabled()) {
			refreshDropDownContents();
		}
	});
	editText.setAdapter(adapter);

	dialog = new AlertDialog.Builder(context).setTitle(R.string.text_download_title).setView(view)
			.setNegativeButton(android.R.string.cancel, null)
			.setPositiveButton(android.R.string.ok, this).create();
	dialog.setOnDismissListener(d -> adapter.shutdown());
	dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
			| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
	dialog.show();
}