Example usage for android.view.inputmethod InputMethodManager toggleSoftInput

List of usage examples for android.view.inputmethod InputMethodManager toggleSoftInput

Introduction

In this page you can find the example usage for android.view.inputmethod InputMethodManager toggleSoftInput.

Prototype

public void toggleSoftInput(int showFlags, int hideFlags) 

Source Link

Document

This method toggles the input method window display.

Usage

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

public void performSearch(String query) {

    LOG.debug("Starting search for: " + query);

    final ProgressDialog searchProgress = new ProgressDialog(context);
    searchProgress.setOwnerActivity(getActivity());
    searchProgress.setCancelable(true);/* w w  w . j  a va 2  s .  c o  m*/
    searchProgress.setMax(100);
    searchProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    final int[] counter = { 0 }; //Yes, this is essentially a pointer to an int :P

    final SearchTextTask task = new SearchTextTask(bookView.getBook());

    task.setOnPreExecute(() -> {

        searchProgress.setMessage(getString(R.string.search_wait));
        searchProgress.show();

        // Hide on-screen keyboard if it is showing
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

    });

    task.setOnProgressUpdate((values) -> {

        if (isAdded()) {

            LOG.debug("Found match at index=" + values[0].getIndex() + ", offset=" + values[0].getStart()
                    + " with context " + values[0].getDisplay());
            SearchResult res = values[0];

            if (res.getDisplay() != null) {
                counter[0] = counter[0] + 1;
                String update = String.format(getString(R.string.search_hits), counter[0]);
                searchProgress.setMessage(update);
            }

            searchProgress.setProgress(bookView.getPercentageFor(res.getIndex(), res.getStart()));
        }
    });

    task.setOnCancelled((result) -> {
        if (isAdded()) {
            Toast.makeText(context, R.string.search_cancelled, Toast.LENGTH_LONG).show();
        }
    });

    task.setOnPostExecute((result) -> {
        searchProgress.dismiss();

        if (!task.isCancelled() && isAdded()) {

            List<SearchResult> resultList = result.getOrElse(new ArrayList<>());

            if (resultList.size() > 0) {
                searchResults = resultList;
                showSearchResultDialog(resultList);
            } else {
                Toast.makeText(context, R.string.search_no_matches, Toast.LENGTH_LONG).show();
            }
        }
    });

    searchProgress.setOnCancelListener(dialog -> task.cancel(true));
    executeTask(task, query);
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

@Override
public void performSearch(String query) {

    LOG.debug("Starting search for: " + query);

    final ProgressDialog searchProgress = new ProgressDialog(context);
    searchProgress.setOwnerActivity(getActivity());
    searchProgress.setCancelable(true);//from  w w w  .  ja  va2 s.c o  m
    searchProgress.setMax(100);
    searchProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    final SearchTextTask task = new SearchTextTask(bookView.getBook()) {

        int i = 0;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            searchProgress.setMessage(getString(R.string.search_wait));
            searchProgress.show();

            // Hide on-screen keyboard if it is showing
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

        }

        @Override
        protected void onProgressUpdate(SearchResult... values) {

            if (!isAdded()) {
                return;
            }

            super.onProgressUpdate(values);

            LOG.debug("Found match at index=" + values[0].getIndex() + ", offset=" + values[0].getStart()
                    + " with context " + values[0].getDisplay());
            SearchResult res = values[0];

            if (res.getDisplay() != null) {
                i++;
                String update = String.format(getString(R.string.search_hits), i);
                searchProgress.setMessage(update);
            }

            searchProgress.setProgress(bookView.getPercentageFor(res.getIndex(), res.getStart()));
        }

        @Override
        protected void onCancelled() {
            if (isAdded()) {
                Toast.makeText(context, R.string.search_cancelled, Toast.LENGTH_LONG).show();
            }
        }

        protected void onPostExecute(java.util.List<SearchResult> result) {

            searchProgress.dismiss();

            if (!isCancelled() && isAdded()) {
                if (result.size() > 0) {
                    searchResults = result;
                    showSearchResultDialog(result);
                } else {
                    Toast.makeText(context, R.string.search_no_matches, Toast.LENGTH_LONG).show();
                }
            }
        };
    };

    searchProgress.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            task.cancel(true);
        }
    });

    task.execute(query);
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

@Override
public void performSearch(String query) {

    LOG.debug("Starting search for: " + query);

    final ProgressDialog searchProgress = new ProgressDialog(context);
    searchProgress.setOwnerActivity(getActivity());
    searchProgress.setCancelable(true);// w ww. j a  va  2s  .c om
    searchProgress.setMax(100);
    searchProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    final SearchTextTask task = new SearchTextTask(bookView.getBook()) {

        int i = 0;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            searchProgress.setMessage(getString(R.string.search_wait));
            searchProgress.show();

            // Hide on-screen keyboard if it is showing
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

        }

        @Override
        protected void onProgressUpdate(SearchResult... values) {

            if (!isAdded()) {
                return;
            }

            super.onProgressUpdate(values);

            LOG.debug("Found match at index=" + values[0].getIndex() + ", offset=" + values[0].getStart()
                    + " with context " + values[0].getDisplay());
            SearchResult res = values[0];

            if (res.getDisplay() != null) {
                i++;
                String update = String.format(getString(R.string.search_hits), i);
                searchProgress.setMessage(update);
            }

            searchProgress.setProgress(bookView.getPercentageFor(res.getIndex(), res.getStart()));
        }

        @Override
        protected void onCancelled() {
            if (isAdded()) {
                Toast.makeText(context, R.string.search_cancelled, Toast.LENGTH_LONG).show();
            }
        }

        protected void onPostExecute(java.util.List<SearchResult> result) {

            searchProgress.dismiss();

            if (!isCancelled() && isAdded()) {
                if (result.size() > 0) {
                    searchResults = result;
                    searchResultWraper.setSearchResult(searchResults);

                    // showSearchResultDialog(result);
                    Intent intent = new Intent(getActivity(), ReadingOptionsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("SELECTED_TAB", 4);
                    intent.putExtras(bundle);
                    startActivity(intent);

                } else {
                    Toast.makeText(context, R.string.search_no_matches, Toast.LENGTH_LONG).show();
                }
            }
        };
    };

    searchProgress.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            task.cancel(true);
        }
    });

    task.execute(query);
}

From source file:com.github.dfa.diaspora_android.activity.MainActivity.java

/**
 * Handle clicks on the optionsmenu/*from  www .ja  v  a 2 s . co  m*/
 *
 * @param item item
 * @return boolean
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    AppLog.i(this, "onOptionsItemSelected()");
    switch (item.getItemId()) {
    case R.id.action_notifications: {
        if (appSettings.isExtendedNotificationsActivated()) {
            return true;
        }
        //Otherwise we execute the action of action_notifications_all
    }
    case R.id.action_notifications_all: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getNotificationsUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_also_commented: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsAlsoCommentedUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_comment_on_post: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsCommentOnPostUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_liked: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsLikedUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_mentioned: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsMentionedUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_reshared: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsResharedUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_notifications_started_sharing: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getSuburlNotificationsStartedSharingUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_conversations: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getConversationsUrl());
            return true;
        } else {
            snackbarNoInternet.show();
            return false;
        }
    }

    case R.id.action_exit: {
        moveTaskToBack(true);
        finish();
        return true;
    }

    case R.id.action_compose: {
        if (WebHelper.isOnline(MainActivity.this)) {
            openDiasporaUrl(urls.getNewPostUrl());
        } else {
            snackbarNoInternet.show();
        }
        return true;
    }

    case R.id.action_search: {
        if (WebHelper.isOnline(MainActivity.this)) {
            final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

            @SuppressLint("InflateParams")
            View layout = getLayoutInflater().inflate(R.layout.ui__dialog_search__people_tags, null, false);
            final EditText input = (EditText) layout.findViewById(R.id.dialog_search__input);
            ThemeHelper.updateEditTextColor(input);
            final DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int which) {
                    String query = input.getText().toString().trim()
                            .replaceAll((which == DialogInterface.BUTTON_NEGATIVE ? "\\*" : "\\#"), "");
                    if (query.equals("")) {
                        Snackbar.make(fragmentContainer, R.string.search_alert_bypeople_validate_needsomedata,
                                Snackbar.LENGTH_LONG).show();
                    } else {
                        openDiasporaUrl(
                                which == DialogInterface.BUTTON_NEGATIVE ? urls.getSearchPeopleUrl(query)
                                        : urls.getSearchTagsUrl(query));
                    }
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                }
            };

            final AlertDialog dialog = new ThemedAlertDialogBuilder(this, appSettings).setView(layout)
                    .setTitle(R.string.search_alert_title).setCancelable(true)
                    .setPositiveButton(R.string.search_alert_tag, clickListener)
                    .setNegativeButton(R.string.search_alert_people, clickListener).create();

            input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        dialog.hide();
                        clickListener.onClick(null, 0);
                        return true;
                    }
                    return false;
                }
            });

            // Popup keyboard
            dialog.show();
            input.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        } else {
            snackbarNoInternet.show();
        }
        return true;
    }
    }

    return super.onOptionsItemSelected(item);
}