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
  • 10,632
  • 4
  • 45
  • 55
MaaAn13
  • 264
  • 5
  • 24
  • 54

6 Answers6

2

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

<LinearLayout
    ...
    android:focusableInTouchMode="true"
    ...
   >
Chintak Patel
  • 748
  • 6
  • 24
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
  • 12,627
  • 3
  • 25
  • 37
0

Try this in your SearchActivity in onCreate

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
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);
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)