Example usage for android.view.inputmethod InputMethodManager toggleSoftInputFromWindow

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

Introduction

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

Prototype

public void toggleSoftInputFromWindow(IBinder windowToken, int showFlags, int hideFlags) 

Source Link

Document

This method toggles the input method window display.

Usage

From source file:Main.java

public static void toggleSoftKeyBoard(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInputFromWindow(view.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
}

From source file:Main.java

public static void openWindowSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(),
            InputMethodManager.SHOW_FORCED, 0);
}

From source file:Main.java

public static void showKeyboard(Context context, View view) {
    try {/*w  ww  .j ava 2  s. com*/
        InputMethodManager inputManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED,
                0);
        view.requestFocus();
    } catch (Exception ex) {
    }
}

From source file:util.Utils.java

/**
 * /*  w w  w  . j av a2s . c  o  m*/
 *
 * @param context   Context
 * @param tokenView View
 */
public static void openInputMethod(Context context, View tokenView) {
    tokenView.setFocusable(true);
    tokenView.requestFocus();
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInputFromWindow(tokenView.getWindowToken(), 0, InputMethodManager.SHOW_FORCED);
}

From source file:com.andrew.apollo.menu.BasePlaylistDialog.java

/**
 * Opens the soft keyboard//w w  w. ja  v a  2s  . c  o  m
 */
protected void openKeyboard() {
    final InputMethodManager mInputMethodManager = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    mInputMethodManager.toggleSoftInputFromWindow(mPlaylist.getApplicationWindowToken(),
            InputMethodManager.SHOW_FORCED, 0);
}

From source file:nu.yona.app.ui.signup.SignupActivity.java

/**
 * Show keyboard.//w w  w . j  a  v  a2s.co m
 *
 * @param editText the edit text
 */
@Override
public void showKeyboard(EditText editText) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(editText.getApplicationWindowToken(),
            InputMethodManager.SHOW_FORCED, 0);
}

From source file:org.mercycorps.translationcards.activity.RecordingActivity.java

private void setFieldBasedOnFocus(boolean hasFocus, EditText field, int hintText) {
    if (hasFocus && field.getText().toString().equals(getString(hintText))) {
        field.setText("");
        field.setTextColor(Color.BLACK);
    } else if (!hasFocus && field.getText().toString().trim().isEmpty()) {
        field.setHint(hintText);//  ww  w .j  a v a 2 s  .  co m
    }
    if (hasFocus) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(field.getApplicationWindowToken(),
                InputMethodManager.SHOW_FORCED, 0);
    }
}

From source file:com.google.dotorg.crisisresponse.translationcards.RecordingActivity.java

private void moveToLabelStep() {
    setContentView(R.layout.recording_label);
    recycleBitmap();//from w  w  w  .  j a v  a 2 s .  c om
    currentBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.recording_label_image);
    currentBitmapView = (ImageView) findViewById(R.id.recording_label_image);
    currentBitmapView.setImageBitmap(currentBitmap);
    final EditText labelField = (EditText) findViewById(R.id.recording_label_field);
    if (label != null) {
        labelField.setText(label);
        labelField.setTextColor(Color.BLACK);
        labelField.setSelection(label.length());
        setLabelNextButtonEnabled(true);
    }
    if (inEditMode) {
        ImageView deleteButton = (ImageView) findViewById(R.id.recording_label_delete_image);
        deleteButton.setVisibility(View.VISIBLE);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbManager dbm = new DbManager(RecordingActivity.this);
                dbm.deleteTranslation(translationId);
                if (!isAsset) {
                    File oldFile = new File(filename);
                    oldFile.delete();
                    if (!savedIsAsset && (savedFilename != null) && !savedFilename.equals(filename)) {
                        oldFile = new File(savedFilename);
                        oldFile.delete();
                    }
                }
                setResult(RESULT_OK);
                finish();
            }
        });
        findViewById(R.id.recording_label_step_marker).setVisibility(View.GONE);
    }
    labelField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus
                    && labelField.getText().toString().equals(getString(R.string.recording_label_hint_text))) {
                labelField.setText("");
                labelField.setTextColor(Color.BLACK);
            } else if (!hasFocus && labelField.getText().toString().equals("")) {
                labelField.setText(getString(R.string.recording_label_hint_text));
                labelField.setTextColor(getResources().getColor(R.color.borderColor));
            }
            if (hasFocus) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInputFromWindow(labelField.getApplicationWindowToken(),
                        InputMethodManager.SHOW_FORCED, 0);
            }
        }
    });
    labelField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Do nothing here.
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0 && !s.equals(getString(R.string.recording_label_hint_text))) {
                setLabelNextButtonEnabled(true);
            } else {
                setLabelNextButtonEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Do nothing here.
        }
    });
    View nextButton = (View) findViewById(R.id.recording_label_next);
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            label = labelField.getText().toString();
            if (label.length() == 0 || label.equals(getString(R.string.recording_label_hint_text))) {
                return;
            }
            moveToAudioStep();
        }
    });
    stepHistory.push(Step.LABEL);
}

From source file:br.ufrgs.ufrgsmapas.libs.SearchBox.java

private void openSearch(Boolean openKeyboard) {
    this.materialMenu.animateState(IconState.ARROW);
    this.logo.setVisibility(View.GONE);
    this.drawerLogo.setVisibility(View.GONE);
    this.search.setVisibility(View.VISIBLE);
    search.requestFocus();//from   www.j  av  a  2  s  . c om
    this.results.setVisibility(View.VISIBLE);
    animate = true;
    results.setAdapter(new SearchAdapter(context, resultList));
    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() > 0) {
                micStateChanged(false);
                mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_clear));
                updateResults();
            } else {
                micStateChanged(true);
                mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_mic));
                if (initialResults != null) {
                    setInitialResults();
                } else {
                    updateResults();
                }
            }

            if (listener != null)
                listener.onSearchTermChanged();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

    });
    results.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            SearchResult result = resultList.get(arg2);
            search(result);

        }

    });
    if (initialResults != null) {
        setInitialResults();
    } else {
        updateResults();
    }

    if (listener != null)
        listener.onSearchOpened();
    if (getSearchText().length() > 0) {
        micStateChanged(false);
        mic.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_clear));
    }
    if (openKeyboard) {
        InputMethodManager inputMethodManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(getApplicationWindowToken(),
                InputMethodManager.SHOW_FORCED, 0);
    }
}

From source file:org.connectbot.ConsoleActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        StrictModeSetup.run();/* w  w  w .j  a va 2 s .com*/
    }

    hardKeyboard = getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY;

    clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    titleBarHide = prefs.getBoolean(PreferenceConstants.TITLEBARHIDE, false);
    if (titleBarHide && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // This is a separate method because Gradle does not uniformly respect the conditional
        // Build check. See: https://code.google.com/p/android/issues/detail?id=137195
        requestActionBar();
    }

    this.setContentView(R.layout.act_console);

    // hide status bar if requested by user
    if (prefs.getBoolean(PreferenceConstants.FULLSCREEN, false)) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    // TODO find proper way to disable volume key beep if it exists.
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    // handle requested console from incoming intent
    if (icicle == null) {
        requested = getIntent().getData();
    } else {
        String uri = icicle.getString(STATE_SELECTED_URI);
        if (uri != null) {
            requested = Uri.parse(uri);
        }
    }

    inflater = LayoutInflater.from(this);

    toolbar = (Toolbar) findViewById(R.id.toolbar);

    pager = (TerminalViewPager) findViewById(R.id.console_flip);
    pager.addOnPageChangeListener(new TerminalViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            setTitle(adapter.getPageTitle(position));
            onTerminalChanged();
        }
    });
    adapter = new TerminalPagerAdapter();
    pager.setAdapter(adapter);

    empty = (TextView) findViewById(android.R.id.empty);

    stringPromptGroup = (RelativeLayout) findViewById(R.id.console_password_group);
    stringPromptInstructions = (TextView) findViewById(R.id.console_password_instructions);
    stringPrompt = (EditText) findViewById(R.id.console_password);
    stringPrompt.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP)
                return false;
            if (keyCode != KeyEvent.KEYCODE_ENTER)
                return false;

            // pass collected password down to current terminal
            String value = stringPrompt.getText().toString();

            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return false;
            helper.setResponse(value);

            // finally clear password for next user
            stringPrompt.setText("");
            updatePromptVisible();

            return true;
        }
    });

    booleanPromptGroup = (RelativeLayout) findViewById(R.id.console_boolean_group);
    booleanPrompt = (TextView) findViewById(R.id.console_prompt);

    booleanYes = (Button) findViewById(R.id.console_prompt_yes);
    booleanYes.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return;
            helper.setResponse(Boolean.TRUE);
            updatePromptVisible();
        }
    });

    Button booleanNo = (Button) findViewById(R.id.console_prompt_no);
    booleanNo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            PromptHelper helper = getCurrentPromptHelper();
            if (helper == null)
                return;
            helper.setResponse(Boolean.FALSE);
            updatePromptVisible();
        }
    });

    fade_out_delayed = AnimationUtils.loadAnimation(this, R.anim.fade_out_delayed);

    // Preload animation for keyboard button
    keyboard_fade_in = AnimationUtils.loadAnimation(this, R.anim.keyboard_fade_in);
    keyboard_fade_out = AnimationUtils.loadAnimation(this, R.anim.keyboard_fade_out);

    keyboardGroup = (LinearLayout) findViewById(R.id.keyboard_group);

    keyboardAlwaysVisible = prefs.getBoolean(PreferenceConstants.KEY_ALWAYS_VISIVLE, false);
    if (keyboardAlwaysVisible) {
        // equivalent to android:layout_above=keyboard_group
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.ABOVE, R.id.keyboard_group);
        pager.setLayoutParams(layoutParams);

        // Show virtual keyboard
        keyboardGroup.setVisibility(View.VISIBLE);
    }

    mKeyboardButton = (ImageView) findViewById(R.id.button_keyboard);
    mKeyboardButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            View terminal = adapter.getCurrentTerminalView();
            if (terminal == null)
                return;
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(terminal.getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
            terminal.requestFocus();
            hideEmulatedKeys();
        }
    });

    findViewById(R.id.button_ctrl).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_esc).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_tab).setOnClickListener(emulatedKeysListener);

    addKeyRepeater(findViewById(R.id.button_up));
    addKeyRepeater(findViewById(R.id.button_up));
    addKeyRepeater(findViewById(R.id.button_down));
    addKeyRepeater(findViewById(R.id.button_left));
    addKeyRepeater(findViewById(R.id.button_right));

    findViewById(R.id.button_home).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_end).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_pgup).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_pgdn).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f1).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f2).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f3).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f4).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f5).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f6).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f7).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f8).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f9).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f10).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f11).setOnClickListener(emulatedKeysListener);
    findViewById(R.id.button_f12).setOnClickListener(emulatedKeysListener);

    actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        if (titleBarHide) {
            actionBar.hide();
        }
        actionBar.addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
            public void onMenuVisibilityChanged(boolean isVisible) {
                inActionBarMenu = isVisible;
                if (!isVisible) {
                    hideEmulatedKeys();
                }
            }
        });
    }

    final HorizontalScrollView keyboardScroll = (HorizontalScrollView) findViewById(R.id.keyboard_hscroll);
    if (!hardKeyboard) {
        // Show virtual keyboard and scroll back and forth
        showEmulatedKeys(false);
        keyboardScroll.postDelayed(new Runnable() {
            @Override
            public void run() {
                final int xscroll = findViewById(R.id.button_f12).getRight();
                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "smoothScrollBy(toEnd[" + xscroll + "])");
                }
                keyboardScroll.smoothScrollBy(xscroll, 0);
                keyboardScroll.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (BuildConfig.DEBUG) {
                            Log.d(TAG, "smoothScrollBy(toStart[" + (-xscroll) + "])");
                        }
                        keyboardScroll.smoothScrollBy(-xscroll, 0);
                    }
                }, 500);
            }
        }, 500);
    }

    // Reset keyboard auto-hide timer when scrolling
    keyboardScroll.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                autoHideEmulatedKeys();
                break;
            case MotionEvent.ACTION_UP:
                v.performClick();
                return (true);
            }
            return (false);
        }
    });

    tabs = (TabLayout) findViewById(R.id.tabs);
    if (tabs != null)
        setupTabLayoutWithViewPager();

    pager.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showEmulatedKeys(true);
        }
    });

    // Change keyboard button image according to soft keyboard visibility
    // How to detect keyboard visibility: http://stackoverflow.com/q/4745988
    contentView = findViewById(android.R.id.content);
    contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            contentView.getWindowVisibleDisplayFrame(r);
            int screenHeight = contentView.getRootView().getHeight();
            int keypadHeight = screenHeight - r.bottom;

            if (keypadHeight > screenHeight * 0.15) {
                // keyboard is opened
                mKeyboardButton.setImageResource(R.drawable.ic_keyboard_hide);
                mKeyboardButton.setContentDescription(
                        getResources().getText(R.string.image_description_hide_keyboard));
            } else {
                // keyboard is closed
                mKeyboardButton.setImageResource(R.drawable.ic_keyboard);
                mKeyboardButton.setContentDescription(
                        getResources().getText(R.string.image_description_show_keyboard));
            }
        }
    });
}