1

my EditText works perfectly fine, until you open another fragment and return back to the first one. After clicking the EditText the keyboard pops up and it looks like the soft-keyboard is pushing the content of the fragment upwards and behind the background image.

This looks something like this:

before

And after you opended a fragment and closed it again:

after

My Search Text View:

<EditText
                android:id="@+id/search_view"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"
                android:background="@drawable/textfield"
                android:digits="@string/digits"
                android:drawableStart="@drawable/ic_search"
                android:drawablePadding="10dp"
                android:fontFamily="@font/opensans_normal"
                android:hint="@string/search"
                android:inputType="text"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:singleLine="true"
                android:textColor="@color/colorFont"
                android:textColorHint="@color/colorFont"
                android:textSize="15sp" />

When this method gets called the bug happens:

  fun addFilter() {
    val topicList = mutableListOf<Topic?>()
    topicAdapter = TopicAdapter(topicList, context, null, true)

    if (CachedData.filter.isNotEmpty()) {

        if (CachedData.filter[0] != null) {
            filterList.visibility = View.VISIBLE
            topicList.add(Topic(CachedData.filter[0], selected = true, clickable = true))
        }

        if (CachedData.filter[1] != null) {
            filterList.visibility = View.VISIBLE
            if (CachedData.filter[0] == null) {
                topicAdapter.setSubselect(-1)
            }
            topicList.add(Topic(CachedData.filter[1], selected = true, clickable = true))
        }
    }
    filterList.adapter = topicAdapter
    filterList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}

UPDATE

This only happens when something is changed in the first Fragment. For example if I add data to the dataset from the RecylerView in the first fragment the bug happens

Lukas
  • 812
  • 1
  • 14
  • 43

3 Answers3

4

Set android:windowSoftInputMode="adjustPan|adjustResize" in the manifest.xml file for the coresponding activity.

Marco
  • 120
  • 1
  • 12
Kuldeep Rathee
  • 282
  • 2
  • 7
2

Hide soft keyboard when opened second fragment. Use this code:

public void hideKeyboard(View view) {
        if (view != null) {
            view.clearFocus();
            InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

and call hideKeyboard() method at onPause() of first fragment like this:

@Override
    public void onPause() {
        super.onPause();
        if (yourEdittext!= null)
            hideKeyboard(yourEdittext);
    }

And windowSoftInputMode to your activity in manifest like this:

        <activity
            android:name=".YourActivity"
            android:windowSoftInputMode="adjustResize|stateAlwaysHidden|adjustPan">
        </activity>
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
2

Simply override method in your activity. It will automatically works in its child fragments as well...

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
    if (currentFocus != null) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
    }
    return super.dispatchTouchEvent(ev)
}

Also Use the following utility functions within your Activities, Fragments to hide the soft keyboard.

fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}

fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}

fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

This will close the keyboard regardless of your code either in dialog fragment and/or activity etc.

Usage in Activity/Fragment:

hideKeyboard()
Jaspalsinh Gohil
  • 941
  • 1
  • 9
  • 20