Example usage for android.view.inputmethod EditorInfo IME_ACTION_PREVIOUS

List of usage examples for android.view.inputmethod EditorInfo IME_ACTION_PREVIOUS

Introduction

In this page you can find the example usage for android.view.inputmethod EditorInfo IME_ACTION_PREVIOUS.

Prototype

int IME_ACTION_PREVIOUS

To view the source code for android.view.inputmethod EditorInfo IME_ACTION_PREVIOUS.

Click Source Link

Document

Bits of #IME_MASK_ACTION : like #IME_ACTION_NEXT , but for moving to the previous field.

Usage

From source file:com.amazon.android.tv.tenfoot.ui.fragments.ContentSearchFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View view = super.onCreateView(inflater, container, savedInstanceState);

    if (view != null) {
        // Set background color and drawable.
        view.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));
        view.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.search_background));

        final SearchBar searchBar = (SearchBar) view.findViewById(R.id.lb_search_bar);
        if (searchBar != null) {

            // Set the left margin of the search bar.
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) searchBar
                    .getLayoutParams();/*w w w.  ja  v a 2 s  .  co m*/

            layoutParams.setMarginStart((int) getResources().getDimension(R.dimen.search_bar_margin_left));

            searchBar.setLayoutParams(layoutParams);

            // Move the search bar items next to the search icon.
            RelativeLayout searchBarItems = (RelativeLayout) searchBar.findViewById(R.id.lb_search_bar_items);

            if (searchBarItems != null) {

                RelativeLayout.LayoutParams searchBarItemsLayoutParams = (RelativeLayout.LayoutParams) searchBarItems
                        .getLayoutParams();

                searchBarItemsLayoutParams.setMarginStart(
                        (int) getResources().getDimension(R.dimen.search_bar_items_margin_left));

                searchBarItems.setLayoutParams(searchBarItemsLayoutParams);

                // Set the search bar items background selector.
                searchBarItems.setBackground(ContextCompat.getDrawable(getActivity(),
                        R.drawable.search_edit_text_bg_color_selector));
            }

            // Set speech orb icon.
            mSpeechOrbView = (SpeechOrbView) searchBar.findViewById(R.id.lb_search_bar_speech_orb);

            if (mSpeechOrbView != null) {
                mSpeechOrbView.setOrbIcon(ContextCompat.getDrawable(getActivity(), R.drawable.search_icon));
            }

            final SearchEditText searchEditText = (SearchEditText) searchBar
                    .findViewById(R.id.lb_search_text_editor);

            if (searchEditText != null) {

                mSearchEditText = searchEditText;

                // Handle keyboard being dismissed to prevent focus going to SearchOrb
                // If user presses back from keyboard, you don't get KeyboardDismissListener
                // so handle that here.
                searchEditText.setOnEditorActionListener((textView, actionId, keyEvent) -> {

                    // Track search if keyboard is closed with IME_ACTION_PREVIOUS or
                    // if IME_ACTION_SEARCH occurs.
                    if (actionId == EditorInfo.IME_ACTION_SEARCH
                            || actionId == EditorInfo.IME_ACTION_PREVIOUS) {

                        if (mQuery != null) {
                            AnalyticsHelper.trackSearchQuery(mQuery);
                        }
                    }

                    if (actionId == EditorInfo.IME_ACTION_PREVIOUS) {

                        // Prevent highlighting SearchOrb
                        mSpeechOrbView.setFocusable(false);
                        mSpeechOrbView.clearFocus();
                        // If there are results allow first result to be selected
                        if (mHasResults) {
                            mSearchEditText.clearFocus();
                        }

                        // Hide keyboard since we are handling the action
                        if (isAdded()) {
                            // Ensure we are added before calling getActivity
                            InputMethodManager inputManager = (InputMethodManager) getActivity()
                                    .getSystemService(Context.INPUT_METHOD_SERVICE);
                            if (inputManager != null) {
                                inputManager.hideSoftInputFromWindow(
                                        getActivity().getCurrentFocus().getWindowToken(),
                                        InputMethodManager.HIDE_NOT_ALWAYS);
                            }
                        } else {
                            Log.e(TAG, "Cannot find activity, can't dismiss keyboard");
                            // Couldn't handle action.
                            // Will expose other focus issues potentially.
                            return false;
                        }
                        // No more processing of this action.
                        return true;
                    }
                    // Someone else needs to handle this action.
                    return false;
                });

                // Override the dismiss listener to get around keyboard issue where dismissing
                // keyboard takes user into first search result's
                // content_details_activity_layout page.
                searchEditText.setOnKeyboardDismissListener(() -> {
                    // If search returns results, focus on the first item in the result list.
                    // If search doesn't have results, this will focus on searchEditText again.
                    mSpeechOrbView.setFocusable(false);
                    mSpeechOrbView.clearFocus();
                    // We don't need to clearFocus on SearchEditText here, the first
                    // result will be selected already.
                });
            }
        }
    }
    return view;
}