26

MY CODE (and more): I have a Coordinator Layout as follows

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include layout="@layout/top" /> <!-- A RelativeLayout -->

            <android.support.v7.widget.Toolbar
                android:id="@+id/MyToolbar"
                android:layout_width="match_parent"
                android:layout_height="64dp"
                app:layout_collapseMode="parallax">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- Main content here -->

    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

Inside the frame layout I am inflating a custom view with an edit text which is placed at the bottom. The edit text and custom view show up fine.

I have defined android:windowSoftInputMode="adjustResize|stateHidden" in the manifest.

MY PROBLEM: While tapping on the edit text to bring up the softkeyboard, it overlaps the content beneath instead of resizing the FrameLayout. I just could not get the FrameLayout to resize when the soft keyboard comes up. Any help will be appreciated

THINGS I TRIED:

  1. Updated design support library to 22.2.1
  2. Added android:layout_gravity="fill_vertical" as an attribute in FrameLayout.
hexhog
  • 261
  • 1
  • 3
  • 5
  • I have this same exact problem. Did you ever figure it out? – Andrew Oct 29 '15 at 21:54
  • @Andrew. I couldn't come up with any hack to make this work. In the end I had to replace Coordinator layout with Relative Layout and made my own custom implementation. – hexhog Oct 30 '15 at 04:32
  • did anyone find its solution yet – Sagar Trehan Jan 17 '16 at 08:50
  • i have the same issue. my edittext and some surrounding elements need to be pushed up when the edittext is selected. adding coordinator layout removed that funtionallity – filthy_wizard Jul 12 '16 at 08:20
  • Hi, I think it is not an issue with CoordinatorLayout I have tried creating the same UI by using ObservableScrollView. I facing the same problem as mentioned. The issue is due to `android:fitsSystemWindows="true"` or `window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)`. https://github.com/ksoichiro/Android-ObservableScrollView – Amrut Bidri Jun 06 '17 at 10:42

9 Answers9

18

This is just another bug in the design support lib and it seems they don't plan to fix it any time soon:

Confirmed, but probably won't be fixed any time soon. adjustResize has a few good use-cases, but using AppBarLayout with CollapsingToolbarLayout with it is not one of them.

It's disturbing that nobody tried to add a EditText to a CoordinatorLayout with android:windowSoftInputMode="adjustResize" while developing the design support lib.

Andreas Wenger
  • 4,310
  • 1
  • 22
  • 31
17

Try this


your_layout.xml

<RelativeLayout
    android:fitsSystemWindows="true" > <!-- Crucial! -->

    <android.support.design.widget.CoordinatorLayout>
        ...
        <android.support.v7.widget.RecyclerView />
        ...
    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

AndroidManifest.xml

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustResize"> <!-- Crucial! -->
</activity>
Michael
  • 9,639
  • 3
  • 64
  • 69
  • Solved my problem. – Ali Behzadian Nejad Jun 24 '16 at 19:34
  • 11
    While this will probably work for most people, it will not let you have a translucent status bar with toolbar content behind it. Instead the status bar will be opaque and content will be below it. – aProperFox Sep 12 '16 at 22:27
  • @aProperFox I have noticed that as well! Thank you for mentioning it. – Michael Sep 12 '16 at 22:55
  • @Michael Thanks, same thing will work for BottomSheetFragment ? Keyboard overlapping Editexts , any solution available !! used getDialog().getWindow(). setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); and your solution, scrollview is scrolling but fields not coming on the top of keyboard Ref: http://stackoverflow.com/questions/39288879/bottom-sheet-fragment-comes-up-with-keyboard – LOG_TAG Feb 17 '17 at 08:08
  • @aProperFox Can confirm. This worked but ends up causing the problem that you mentioned. – Lee Kang Jul 10 '18 at 21:59
10

I'm using this for solving CoordinatorLayout bug

in onCreateDialog in BottomSheetFragment

KeyboardUtil(getActivity(), view);

or

For Activity use

new KeyboardUtil(this, findViewById(R.id.fragment_container));

by using this Util class

Java version: https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java

Kotlin version: https://gist.github.com/agustinsivoplas/6f80233699aebebd512542503ea32682 Credit:Mikepenz,Agustinsivoplas

ADM
  • 20,406
  • 11
  • 52
  • 83
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
7

My Layout

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout>

            <ImageView>

            <android.support.v7.widget.Toolbar>

        <android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton>

<android.support.design.widget.CoordinatorLayout>

I had the exact same problem. I figured out, that if i use something within the e.g. NestedScrollView or within a FrameLayout i would assume as well that handles the Scroll itself, the CoordinatorLayout does not adjustResize / behave the way you want. The adjustResize|adjustPan settings within the Manifest are ignored.

For me this was very hard to figure out, because i wrote a few (combined-) Views myself that contained RelativeLayout or ScrollViews and such.

If i just avoid using RelativeLayouts or anything else than NestedScrollView to handle the view scrolling, it behaves the way, at least i wanted it to.

But i think, there is a Bug within CoordinatorLayout or a behaviour we all need to understand better to avoid this type of problems...

Maybe this is useful to know for someone...

My Settings are:

  • support/design/appcompat : 22.2.1
  • no need for layout_gravity
SukiSoft
  • 131
  • 3
  • 7
  • so essentially what you are saying is use only a NestedScrollview beneath the AppBarLayout? – filthy_wizard Jul 12 '16 at 08:30
  • 1
    If you can, upgrade to a newer version of the support lib. I now have upgraded to 23.x and I am not facing any problems any more. Except for the anchor gravity which has to be set to a view other than the Coordinator Layout itself. – SukiSoft Jul 14 '16 at 20:21
  • I had a similar problem and the gist of the fix is to use a NestedScrollView as a CoordinatorLayout child. I didn't get `adjustPan` to work this way but at least the default behavior (`adjustResize` I believe) started working correctly. Thanks for pointing me in the right direction! – pajton Jan 02 '17 at 15:26
6

Don't use CoordinatorLayout as a root layout if you use EditText views on your NestedScrollView

<androidx.constraintlayout.widget.ConstraintLayout
  android:fitsSystemWindows="true">

  <androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.material.appbar.AppBarLayout>

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

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

    <androidx.core.widget.NestedScrollView>
    <Editext/>
    <androidx.core.widget.NestedScrollView/>

  </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
shehan gamage
  • 59
  • 1
  • 5
2

Hi I had the same problem and I only put

        android:windowSoftInputMode="adjustPan"

in yout manifest for the activity concerned and the keyboard moves up the view accordingly now

Joxad
  • 69
  • 3
2

adjustResize not work with

app:layout_behavior="@string/appbar_scrolling_view_behavior"

Remove this line:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 
Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
1

A shortened version from KeyboardUtil, add this to your activity:

getWindow().getDecorView().getViewTreeObserver()
        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        Rect r = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        rootView.setPadding(0, 0, 0,
                activity.getResources().getDisplayMetrics().heightPixels - r.bottom);
    }
});
Tauno Siitam
  • 485
  • 4
  • 12
  • I used this listener and whilst it fixed that bug we are all having it removed the bottom 60dp of my fragment view which contains a "complete" button. If i change the last line : activity.getResources().getDisplayMetrics().heightPixels - r.bottom and replace it with 0 I get my full view back but then the listener doesnt work with the edit texts. Is there a way to edit this so It doesnt cut out the bottom of my view and still works with edit texts? – AndroidDev123 Dec 21 '20 at 23:44
0

For now I am solving it using androidx.drawerlayout.widget.DrawerLayout to wrap:

CoordinatorLayout, AppBarLayout, CollapsingToolbarLayout, Toolbar, NestedScrollView

It is maybe not the best idea but it is working for me ;)

I have a sample in my project: https://github.com/maiconpintoabreu/Kupping-Mobile/blob/master/app/src/main/res/layout/activity_event_new.xml

Maicon Santana
  • 340
  • 1
  • 7