Android Toolbar moves up when keyboard appears
R

17

39

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>

enter image description here

Revisionist answered 18/9, 2015 at 10:26 Comment(0)
R
21

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

Revisionist answered 11/10, 2015 at 7:6 Comment(6)
does not work for me, In general when you have a bg inside this screen is hard to manage, just adjustPan not rezize your background. With Background and status bar fixed , this does not work. Im searching here to fix this , but im not found anything yet.Tissue
After adding this code my screen is adjusted perfectly, but i'm getting a white patch i.e something like margin bottom in my toobar.. How to avoid that.?Liddle
This code works. but below and above toolbar it shows white statusbarMesser
This doesn't work, I'm using AppBarLayout, ToolBar and NestedScrollViewPalacio
What is an "activity's parent"? Did you mean the activity root layout? The 's means is possessive in English so I didn't know an activity had a parent that wasn't part of the Android system.Monck
using adjustResize, u will lose animation when keyboard show, that's bad choiceCytolysin
S
14

Or add this line in manifest. android:windowSoftInputMode="adjustResize"

Sultan answered 18/9, 2015 at 11:14 Comment(1)
This is bad solution because using adjustResize your page with textedit will not scroll to the elementPalacio
C
12

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.

Cameleer answered 18/9, 2015 at 10:32 Comment(2)
Thanks for your time. I've tried the adjustResize also it didn't work. Am I missing something else?Revisionist
Therefore there must be some issue with your layout. And to be honest I don't really get the layout file you provided. Why is your RecyclerView wrapped inside a LinearLayout? Also, since you've decided to use a RelativeLayout as a root, try setting android:layout_alignParentTop="true for your Toolbar.Cameleer
B
9

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:

  1. Changed the root layout from Coordinator Layout to RelativeLayout
  2. Included android:fitsSystemWindows="true" to the root layout
  3. Included android:windowSoftInputMode="adjustResize|stateAlwaysHidden" to the activity manifest tag.
Beeswax answered 17/12, 2016 at 18:44 Comment(0)
Z
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.

Zoochemistry answered 31/3, 2016 at 13:54 Comment(0)
S
3

enter image description here

Here is the solution for this fix

  • android:windowSoftInputMode="adjustResize" in Manifest File
  • Make 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)
    
Spokeshave answered 21/1, 2019 at 11:1 Comment(1)
Screen flickers when i use this fix.Ledford
S
2
<include
    android:id="@+id/toolbar_home"
    layout="@layout/toolbar_home"
    android:elevation="5dp"
android:layout_alignParentTop="true"
    />
Sultan answered 18/9, 2015 at 11:11 Comment(0)
J
1

Please check WindowsSoftInputMode in Android Manifest try to set AdjustResize or AdjsutPan

Jojo answered 18/9, 2015 at 13:3 Comment(0)
C
1
  1. Include android:fitsSystemWindows="true" to the root in xml file of the activty.
  2. Include android:windowSoftInputMode="stateHidden|adjustResize" to the activity manifest tag.

These two steps solved my problem.

Camouflage answered 20/7, 2021 at 8:47 Comment(0)
H
0

try this,

android:windowSoftInputMode="adjustPan"

in your manifest.xml in activity tag.

Holloweyed answered 18/9, 2015 at 13:26 Comment(0)
M
0

Using android:windowSoftInputMode="adjustNothing" on the activity in the manifest worked for me.

Mess answered 31/7, 2017 at 18:57 Comment(0)
P
0

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
Pteridology answered 26/5, 2018 at 14:16 Comment(0)
B
0

If NOTHING work, be aware that libraries can manipulate soft input, for example com.arlib.floatingsearchview.

enter image description here

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);
}
Bankrupt answered 18/6, 2020 at 8:46 Comment(0)
G
0

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>
Gwenette answered 29/8, 2020 at 6:5 Comment(0)
N
0

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">
Nela answered 8/10, 2021 at 11:56 Comment(0)
E
0

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

Eu answered 2/4, 2022 at 12:25 Comment(0)
L
-1

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.

Lecia answered 16/10, 2017 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.