-2

Note: MY question is not about losing focus, it's about gaining focus, hence it is not a duplicate of the marked question

I have a search menuItem on my toolbar which, on click will open up a search layout ontop of it and allow user to search. I have tried the following solutions mentioned elsewhere in Stack Overflow but none of them seems to work. (I should mention that due to it being used elsewhere I can't edit the XML)

from here

    searchText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
  1. This just pops up the keyboard, nothing happens I have tried this + editText.setCursorVisible(true);

  2. With various combinations of setFocusable(true) and setFocusableInTouchMode(true); before the aformentioned

     searchText.setFocusable(true);
     searchText.setFocusableInTouchMode(true);
     searchText.requestFocus();
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
    
  3. Doing (1) in separate thread (using Handler.post)

     Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                editText.requestFocus();
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            }
        },100);
    
  4. Calling editText.performClick()

  5. Clearing previous focus like so:

    private void clearFocus() {
    if (activity.getCurrentFocus() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getApplication().getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        activity.getCurrentFocus().clearFocus();
    }
    }
    

My code sample:

public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getItemId() == R.id.action_search) {
        menuItem.setVisible(false);
        onClickForSearchMenuItem();
        return true;
    }
    return super.onOptionsItemSelected(menuItem);
}

 private void onClickForSearchMenuItem() {
    if (searchLayoutInToolbar != null) {
        searchLayoutInToolbar.setVisibility(View.VISIBLE);
        final EditText editText = (EditText) searchLayoutInToolbar.findViewById(R.id.custom_Search_Layout_Search_EditText);
        //clearFocus();
        //editText.setFocusableInTouchMode(true);
        //editText.setFocusable(true);
        editText.requestFocus();

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        activity.invalidateOptionsMenu();
    }
}

The developer pages at android point out that

A View will not take focus if one of its parents has getDescendantFocusability() equal to FOCUS_BLOCK_DESCENDANTS

but none of the parents of my view are set to that

ColonD
  • 954
  • 10
  • 28
  • Possible duplicate of [Force EditText to lose focus when: some keyboard keys are pressed and when user clicks on something else in the activity](https://stackoverflow.com/questions/40325325/force-edittext-to-lose-focus-when-some-keyboard-keys-are-pressed-and-when-user) – Vidhi Dave Oct 26 '17 at 05:59
  • I read that, but it's about losing focus, not gaining it. Even if he explains how he got focus in the first place, he does it in xml, and not programatically. – ColonD Oct 26 '17 at 06:06
  • try this https://stackoverflow.com/a/8991563/8089770 – Vidhi Dave Oct 26 '17 at 06:07
  • As I said in my question, I have already tried that. and my keyboard does pop up, but the edittext is not focused – ColonD Oct 26 '17 at 06:10

1 Answers1

2

Try This.... I hope, this may help you.

searchText.setFocusableInTouchMode(true);
searchText.requestFocus();

this might be required on some phones (some of the older devices):

final InputMethodManager inputMethodManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);

If not worked, try field.requestFocus() in the onResume() method of the activity (instead of the onCreate()).

Thanks.

  • As I said in my question, I have already tried the above. also, I am requesting focus only when user clicks on the search menuItem, as the edittext is not visible until that point – ColonD Oct 26 '17 at 06:07
  • so, when user clicks on the search MenuItem, try calling the code, I have said in my answers. Thanks –  Oct 26 '17 at 06:13
  • I did do that before asking the question and it did not work! – ColonD Oct 26 '17 at 06:14
  • It should work....... but as it is not working, I think, If possible , you should paste the codes in the class. Thanks –  Oct 26 '17 at 06:19