0
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:drawableLeft="@drawable/search"
    android:background="@drawable/search_bar"
    android:paddingLeft="15dp"
    android:drawablePadding="10dp"
    android:inputType="text"
    android:id="@+id/srchbar"
    android:onClick="searchBarClick">
</EditText>
public void searchBarClick(View view){

}

I have an edit text box and onClick function.Please tell how to show keyboard ONLY when user click on edittext box to insert data.In normal condition,keyboard appears as i run my application but i only want it to be shown when user click on edittext box.

2 Answers2

1

add this below code in the root element of your layout.

 <LinearLayout
     android:layout_width="0px"
     android:layout_height="0px"
     android:focusable="true"
     android:focusableInTouchMode="true" />
Sachin
  • 3,815
  • 2
  • 13
  • 26
0
editText.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }, 50);

sometime edittext might get focus after manually hide soft-keyboard.

so, delay some time and then after hide it forcefully in onResume() method of your activity or fragment

Akash Pal
  • 754
  • 4
  • 14