1

I have a Fragment with an EditText, when I click on it the soft keyboard shows up which is OK.. Now I want to keep this keyboard visible no matter what until I press the Back button. Currently, whenever I click outside the EditText the keyboard hides immediately, I don't want that.

My Fragment XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/conversation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/myList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:divider="@null"
    android:paddingBottom="2dp"
    android:transcriptMode="alwaysScroll"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent"
    tools:layout_editor_absoluteX="0dp" />

<LinearLayout
    android:id="@+id/controlsLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteX="0dp">

        .....


            <EditText
                android:id="@+id/messageInput"
                android:layout_width="0dp"
                android:layout_height="44dp"
                android:inputType="textAutoComplete|textMultiLine"
                android:paddingEnd="1dp"
                android:paddingStart="3dp"
                android:textColor="@android:color/black" />

        .....


</LinearLayout>

Note: I don't want to intercept Back button. I want to keep the Soft Input Keyboard visible even if I click out side the EditText, That's all.

Abuzaid
  • 853
  • 8
  • 28
  • Possible duplicate of [Intercept back button from soft keyboard](https://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard) –  Nov 02 '17 at 15:38

1 Answers1

0

You can use this to hide the keyboard when the back button is pressed :

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

To disable the feature which hide the keyboard when focus is lost, I think that you can add a listener on your Fragment that handle the touch on the screen and do nothing :

View view = inflater.inflate(R.layout.fragment_test, container, false);

view.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            // Do nothing
            return true;
        }
});

Hope it helps !

Maxouille
  • 2,729
  • 2
  • 19
  • 42