I have the below code in layout. I have the SearchView
at the bottom of RelativeLayout
I want to bring the searchview on top of keyboard when it gets focus
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:foregroundGravity="top"
android:layout_width="match_parent"
android:layout_height="530dp"
android:layout_alignParentTop="true"
android:transcriptMode="alwaysScroll"
/>
<android.support.v7.widget.SearchView
android:id="@+id/searchview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/recyclerview"
android:layout_alignParentBottom="true"
android:iconifiedByDefault="false"
/>
</RelativeLayout>
I tried the below code but it doesnt work as expected
Isnt Android suppose do it by default?
searchview.FocusChange += Searchview_FocusChange;
private void Searchview_FocusChange(object sender, View.FocusChangeEventArgs e)
{
if (e.HasFocus)
{
searchview.Parent.BringChildToFront(searchview);
searchview.BringToFront();
}
}
How do I fix this? I tried some options mentioned in other posts with no luck.
As a work around I added below code, but I do not feel comfortable with this
searchview.Click += Searchview_Click;
private void Searchview_Click(object sender, System.EventArgs e)
{
if (sender is SearchView searchview)
{
searchview.Parent.BringChildToFront(searchview);
searchview.BringToFront();
searchview.SetY(searchview.GetY() - 100);
searchview.SetY(searchview.GetY() + 100);
}
}
You should put the RelativeLayout in a ScrollView
<ScrollView
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:fitsSystemWindows="true"
android:scrollbars="vertical">
<RelativeLayout
//...
</ScrollView>
And set the windowSoftInputMode in Manifest.xml
android:windowSoftInputMode="adjustPan|stateHidden"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.