Wednesday, September 6, 2017

Different behaviours with ADJUST_RESIZE in BottomSheetDialogFragment

Leave a Comment

I'm trying to create a BottomSheetDialogFragment with a TextInputLayout inside. I'm setting this BottomSheet as adjustResize to prevent the keyboard cover the TextInputLayout. The thing is that I'm getting different behaviours with different android versions.

This is the layout:

<android.support.constraint.ConstraintLayout 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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent">  <android.support.design.widget.TextInputLayout     android:id="@+id/textInputLayout"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_marginBottom="16dp"     android:layout_marginEnd="16dp"     android:layout_marginStart="16dp"     android:layout_marginTop="16dp"     android:background="@android:color/darker_gray"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toStartOf="parent"     app:layout_constraintTop_toTopOf="parent"     app:layout_constraintVertical_bias="0.0">      <android.support.design.widget.TextInputEditText         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="hint" /> </android.support.design.widget.TextInputLayout> 

This the BottomSheetDialogFragment:

public class TestFragment extends BottomSheetDialogFragment {  @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {      getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);      return inflater.inflate(R.layout.fragment_test, container, true); } 

}

This is the desired result:

enter image description here

And this is the result that I'm getting in some versions:

enter image description here

I'm getting the desired result with:

  • Galaxy S6 with Android 7.0
  • Emulator with <= Android 5.1

And no desired result with:

  • Nexus 5 with Android 7.1.2 (LineageOS 14.1)
  • Emulator with => Android 6.0

Does anyone know why this happen or how to solve it?

Thanks in advance!!!

Cheers.

1 Answers

Answers 1

This issue wasted my 1 hour.Don't use ADJUST_RESIZE.

Simply wrap your layout inside a NestedScrollView.

Nothing More.Nothing less.Problem solved.

Here is the XML

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView 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.support.constraint.ConstraintLayout         android:id="@+id/linearLayout"         android:layout_width="match_parent"         android:layout_height="match_parent">          <android.support.design.widget.TextInputLayout             android:id="@+id/textInputLayout"             android:layout_width="0dp"             android:layout_height="wrap_content"             android:layout_marginBottom="16dp"             android:layout_marginEnd="16dp"             android:layout_marginStart="16dp"             android:layout_marginTop="16dp"             android:background="@android:color/darker_gray"             app:layout_constraintBottom_toBottomOf="parent"             app:layout_constraintEnd_toEndOf="parent"             app:layout_constraintStart_toStartOf="parent"             app:layout_constraintTop_toTopOf="parent"             app:layout_constraintVertical_bias="0.0">              <android.support.design.widget.TextInputEditText                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:hint="hint" />         </android.support.design.widget.TextInputLayout>      </android.support.constraint.ConstraintLayout>  </android.support.v4.widget.NestedScrollView> 

Here is the source code

public class DemoFragment extends BottomSheetDialogFragment {      @Override     public void onCreate(@Nullable Bundle savedInstanceState) {         super.onCreate(savedInstanceState);     }      private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {          @Override         public void onStateChanged(@NonNull View bottomSheet, int newState) {             if (newState == BottomSheetBehavior.STATE_HIDDEN) {                 dismiss();             }         }          @Override         public void onSlide(@NonNull View bottomSheet, float slideOffset) {         }     };      @Override     public void setupDialog(Dialog dialog, int style) {         super.setupDialog(dialog, style);         View contentView = View.inflate(getContext(), R.layout.fragment_layout, null);         dialog.setContentView(contentView);          CoordinatorLayout.LayoutParams layoutParams =                 (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();         CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();         if (behavior != null && behavior instanceof BottomSheetBehavior) {             ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);         }     }  } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment