'Android Toolbar moves up when keyboard appears
I'm creating a chat based UI screen where I have toolbar and recyclerview for chat messages, and reply msg layout.
Whenever edittext get focus It moves up the toolbar. Instead I would like to resize the recyclerview.
some of the stackoverflow answers suggest to place a empty scrollview below the toolbar, but It didn't work.
<activity
android:name=".PostMessageActivity"
android:label="@string/title_activity_post_message"
android:windowSoftInputMode="stateVisible|adjustResize"
>
</activity>
I am setting the windowSoftInputMode to stateVisible|adjustPan in the manifest file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pasonet.yokibu.PostMessageActivity"
>
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home"
android:elevation="5dp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_home"
android:orientation="vertical"
android:layout_above="@+id/add_post_layout"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/post_msg_recyclerview"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:id="@+id/add_post_layout"
android:background="#ffffff"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:elevation="5dp"
android:layout_margin="0pt"
>
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/messageText"
android:layout_gravity="bottom"
android:layout_weight="1"
android:maxLines="4"
android:scrollbars="vertical"
android:background="@color/trasnperant"
android:hint="Type your message"
android:layout_marginBottom="4dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_send_black_36dp"
android:id="@+id/sendButton"
android:background="@drawable/abc_btn_default_mtrl_shape"
android:onClick="addPost"
/>
</LinearLayout>
Solution 1:[1]
The problem was that I was using <item name="android:windowTranslucentStatus">true</item> in styles.xml
To enable adjustResize add android:fitsSystemWindows="true" in your activity's parent layout
Solution 2:[2]
Or add this line in manifest. android:windowSoftInputMode="adjustResize"
Solution 3:[3]
If you don't want your Toolbar to be pushed up, you shouldn't be using adjustPan. Using adjustResize without any other things (without adding any additional Views) should be enough.
Solution 4:[4]
In my case, my default application was using Coordinator Layout for the root element. Even doing what was described above wasn't solving my problem. Then I did the following:
- Changed the root layout from Coordinator Layout to RelativeLayout
- Included
android:fitsSystemWindows="true"to the root layout - Included
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"to the activity manifest tag.
Solution 5:[5]
You should add android:fitsSystemWindows="true" in your toolbar's root layout. If you add it in your activity's parent layout, a strange line (with status bar height) will appear above your toolbar.
Solution 6:[6]
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home"
android:elevation="5dp"
android:layout_alignParentTop="true"
/>
Solution 7:[7]
Here is the solution for this fix
android:windowSoftInputMode="adjustResize"in Manifest FileMake a class "CommentKeyBoardFix.Java" and copy and paste the below code.
public class CommentKeyBoardFix { private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; private Rect contentAreaOfWindowBounds = new Rect(); public CommentKeyBoardFix(Activity activity) { FrameLayout content = activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent); frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); } private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int heightDifference = 0; if (heightDifference > (usableHeightNow /4)) { // keyboard probably just became visible frameLayoutParams.height = usableHeightNow - heightDifference; } else { // keyboard probably just became hidden frameLayoutParams.height = usableHeightNow; } mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom); mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds); return contentAreaOfWindowBounds.height(); } }And then call this class in your Activity or Fragment; for Kotlin:
setContentView(R.layout.your_comments_layout) CommentKeyBoardFix(this)or for Java:
new CommentKeyBoardFix(this)
Solution 8:[8]
Please check WindowsSoftInputMode in Android Manifest try to set AdjustResize or AdjsutPan
Solution 9:[9]
- Include
android:fitsSystemWindows="true"to the root in xml file of the activty. - Include
android:windowSoftInputMode="stateHidden|adjustResize"to the activity manifest tag.
These two steps solved my problem.
Solution 10:[10]
try this,
android:windowSoftInputMode="adjustPan"
in your manifest.xml in activity tag.
Solution 11:[11]
Using android:windowSoftInputMode="adjustNothing" on the activity in the manifest worked for me.
Solution 12:[12]
I tried many ways to keep the toolbar fixed , while not resizing or compressing the layout by android:windowSoftInputMode="adjustResize" , that is not a proper .
That is only possible by Coordinator layout ,
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:id="@+id/tv_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
ripple:contentInsetStart="0dp">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:id="@+id/coordinate_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:marginTop="56dp">
</ScrollView>
Keep the structure as
Coordinator layout
|-->Toolbar (android:id="@+id/toolbar")
|-->ScrollView (android:layout_below="@id/toolbar")
|-->Child
|-->Child
|-->Child
Solution 13:[13]
If NOTHING work, be aware that libraries can manipulate soft input, for example com.arlib.floatingsearchview.
What happen is, for example you have list view with search view, when you show the fragment with floatingsearchview its set soft input to SOFT_INPUT_ADJUST_PAN(you may not care about it in fragment with list view), but if the next fragment will be item details with many edit texts it will push the toolbar up.
In my case I cannot replace this library so i set SOFT_INPUT_ADJUST_RESIZE in the base fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
Solution 14:[14]
In my case I had to make windowFullscreen to false in styles.xml in combination with above answer to make it work
<item name="android:windowFullscreen">false</item>
Solution 15:[15]
I added this
fitsSystemWindows
inside scrollview and it solved:
<ScrollView
android:id="@+id/rootScrollview"
android:layout_width="0dp"
android:layout_height="0dp"
android:fitsSystemWindows="true">
Solution 16:[16]
I initially had android:windowSoftInputMode="adjustPan" on the AndroidManifest for activity with toolbar, a chat message recycler view and input on the bottom.
This was getting desired result of recycler view going up on input view receiving focus and keyboard opening up but the Toolbar was hiding
After trying out a bunch of solutions, what worked for me was:
No windowSoftInputMode at all
and
app:stackFromEnd="true" on the recycler view
Solution 17:[17]
I use a CoordinatorLayout as root Layout for the toolbar. I had the same problem but solved it by setting the toolbar to:
app:layout_scrollFlags="enterAlways"
instead of:
app:layout_scrollFlags="scroll|enterAlways"
and adding:
android:windowSoftInputMode="adjustNothing"
in the activity in the manifest.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow



