EditText Remove Focus - Hide & Show Keyboard

EditText have the highest priority of focus. Keyboard quick show up, when an Activity have an EditText in any place without in notice of others view of it. When we need to remove focus or to show keyboard when required, we have two methods to follow for achieving it.
Remove EditText Focus
When remove the focus of EditText, keyboard will automatically hide. To do this, add this attribute android:focusableInTouchMode="false" to the EditText tag as

<EditText
  android:id="@+id/radius"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:focusableInTouchMode="false"
  android:layout_marginLeft="20dp"
  android:layout_marginTop="10dp"
  android:layout_marginRight="20dp"
  android:hint="Enter Distance in Meters"
  android:inputType="number"
  android:paddingLeft="15dp"
  android:paddingTop="10dp"
  android:paddingBottom="10dp"
  android:textColor="#faf8f4"
  android:textColorHint="#faf8f4"
  android:textSize="15sp" />



Hide or Show Keyboard Programmatically
We can also hide and show keyboard programmatically when needed as

void hideKeyboard(Activity activity) {
  View view = activity.getCurrentFocus();
  InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  assert manager != null && view != null;
  manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

void showKeyboard(Activity activity) {
  View view = activity.getCurrentFocus();
  InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  assert manager != null && view != null;
  manager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}

Comments