3

I have a situation where I open SearchActivity and I want to have cursor in SearchView (keeping focus) while not showing keyboard at first. When and ONLY when user presses SearchView I want to show keyboard (cursor should be intact)

Quick learner
  • 9,144
  • 3
  • 37
  • 51
MaaAn13
  • 1,008
  • 3
  • 18
  • 46

6 Answers6

2

Add this below line in your activity.xml file's main layout

<LinearLayout
    ...
    android:focusableInTouchMode="true"
    ...
   >
Chintak Patel
  • 708
  • 5
  • 22
0

Add this line in Manifest inside SearchActivity

<activity name=".SearchActivity"
android:windowSoftInputMode="stateHidden" />

or In Activity onCreate() add

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Rajan Kali
  • 11,436
  • 3
  • 24
  • 35
0

Try this in your SearchActivity in onCreate

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;
Santanu Sur
  • 9,980
  • 7
  • 27
  • 48
0

add this to your onCreate to hide the kayboard

public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}
diksha
  • 109
  • 8
0

you used below code to show key board..

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);

and close keyboard used this ...

        imm.hideSoftInputFromWindow(searchView.getWindowToken(),0);
Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49
0

You gotta hide the softkeyboard after the SearchView gets focus:

search.postDelayed({
        val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view?.windowToken, 0)
    }, 100)