Funny Moments
Funny Moments

Reputation: 451

Soft-keyboard dissapears after rotation

I've got a simple fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/BrightYellowCrayola"
>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tbDriver"
        app:navigationIcon="@drawable/baseline_menu_24"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        >

        <androidx.appcompat.widget.SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:defaultQueryHint="@string/search_drivers"
            app:iconifiedByDefault="false"
            app:searchIcon="@null"
            app:queryBackground="@android:color/transparent"
            app:submitBackground="@android:color/transparent"

            />

    </androidx.appcompat.widget.Toolbar>



</com.google.android.material.appbar.AppBarLayout>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingStart="@dimen/zero_margin_when_normal"
        android:paddingEnd="@dimen/zero_margin_when_normal" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/efab_recycler_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="@color/BrightYellowCrayola"
    android:src="@drawable/baseline_person_add_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

The issue is that after I enter a text in my serchfield and I rotate the phone, the searchview keeps focus (keyboard) for a short period of time and then dissapears again as seen in this example

NOTE!: However, the "funny" thing is that this only happens if I rotate the phone horizontally, when I orientate my phone verticaly, the soft-keyboard appears again.

Upvotes: 0

Views: 240

Answers (2)

Jenej Fjrjdn
Jenej Fjrjdn

Reputation: 347

Try adding the following line in your searchview:

android:imeOptions="flagNoExtractUi"

So the endresult should look like this:

<androidx.appcompat.widget.SearchView
    android:id="@+id/svDriver"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:defaultQueryHint="@string/search_drivers"
    app:iconifiedByDefault="false"
    app:searchIcon="@null"
    app:queryBackground="@android:color/transparent"
    app:submitBackground="@android:color/transparent"
    android:imeOptions="flagNoExtractUi"

    />

Upvotes: 1

Rinki Devi
Rinki Devi

Reputation: 71

Add the below line in your manifest.xml file

 <activity
            android:name=".MyActivity"
            android:windowSoftInputMode="stateUnchanged|adjustPan" >

"stateUnchanged" will make the state of keyboard same as it was in earlier state. Either hidden or visible.

"adjustResize" will make your edit text visible.

Upvotes: 0

Related Questions