Frank Xu
Frank Xu

Reputation: 11

Android EditText Keyboard making Fragment disappear

In one of the fragments in my app I have an edittext, and whenever I click on it the keyboard comes up and all the content in the fragment just disappears, including the edittext. I can still type on the keyboard, and after I press the back button, everything comes back and whatever I type is showing on the edittext, but how do I stop everything from disappearing?

Below is the code for the fragment and its layout.

Fragment:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PaymentsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_payments, container, false);
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:hint="Enter Something"

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button2"

    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="Button"

    app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/editText2"

    android:layout_marginLeft="0dp"
    android:layout_marginRight="16dp" />

</android.support.constraint.ConstraintLayout>

Upvotes: 1

Views: 1025

Answers (3)

Benoit
Benoit

Reputation: 1921

I still reproduce it in the case of a "Multiline EditText in Scrollview in fragment in ConstraintLayout" even on Android 8. Using a SingleLine does not create any issue.

It is happening because the dynamic height of a EditText does not work well in this situation. This solution was hackish, but it does work like a charm if you can afford to fix the number of lines:

  • Create a custom class LabelEditView extending EditText
  • Add an attribute
  <declare-styleable name="LabelEditView">
    <attr name="label_edit_line_count" format="integer" />
  </declare-styleable>
  • In the constructor call:
setLineCount(attributes.getInt(R.styleable.LabelEditView_label_edit_line_count,

1));

Then, finally, the interesting piece of code:

  /**
   * Set the number of visible lines. Does not support to switch between modes or font
   */
  public void setLineCount(int lineCount) {
    if (lineCount <= 1) {
      setSingleLine(true);
    } else {
      setSingleLine(false);
      setImeOptions(EditorInfo.IME_ACTION_DONE);
      setRawInputType(InputType.TYPE_CLASS_TEXT);

      // Fix around EditText in Scrollview in fragment in ConstraintLayout
      Paint.FontMetrics fm = getPaint().getFontMetrics();
      int textHeight = (int) ((fm.bottom - fm.top) * (lineCount + 0.5));
      setMaxHeight(textHeight);
      setHeight(textHeight);
    }
  }

Upvotes: 0

Andre Luis Pereira
Andre Luis Pereira

Reputation: 1

Put in AndroidManifest.xml (in your activity):

<activity android:name="(activity name)" android:windowSoftInputMode="adjustPan">

Upvotes: -1

Vidhi Dave
Vidhi Dave

Reputation: 5684

Reference : Developer documentation

android:windowSoftInputMode

The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

adjustResize

The activity's main window is always resized to make room for the soft keyboard on screen.

adjustPan

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

Use this way in manifest :

<activity android:windowSoftInputMode="adjustResize"> </activity>

Upvotes: 2

Related Questions