24

BottomSheetDialog's background is blinking when switching between apps. What am I doing wrong ?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        findViewById(R.id.btn1).setOnClickListener(v -> {
            BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
            bottomSheetDialog.setContentView(R.layout.content);
            bottomSheetDialog.show();
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

content.xml

<?xml version="1.0" encoding="utf-8"?>
<View 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="300dp"
    android:background="#F00" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.holtaf.testandroidapplication">

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My appcompat version is 27.1.1.

enter image description here

Stakshi
  • 389
  • 1
  • 15
holtaf
  • 787
  • 6
  • 19
  • Can you post your complete code of the activity and its xml – RoyalGriffin Jun 29 '18 at 09:28
  • OnResume your bottom sheet is getting refreshed. You might want to check that. – mark922 Jun 29 '18 at 09:30
  • @RoyalGriffin I have included my activity_main.xml also. This is the whole code I'm using right now. – holtaf Jun 29 '18 at 09:52
  • I will investigate more, but my first impression is BottomSheetDialog class redraw the layout while pausing and starting. Time which is taken while pausing and starting it because of animation which BottomSheetDialog is having. – Jitesh Mohite Jul 01 '18 at 17:56
  • have you put some code in onResume of your activity, if not then try to put some buttons etc inside bottomsheet and remove the red color, then see what happens – Abdul Aziz Jul 02 '18 at 12:39
  • @AbdulAziz I have posted MainActivity as it is and adding buttons to bottom sheet or removing the red color doesn't have any effects, it's still buggy. – holtaf Jul 04 '18 at 07:36
  • @holtaf I think it's bug of android OS. Because if you try this with the system dialog you will get this same effect. Even i saw it on Google Play Music app also. – Moinkhan Jul 06 '18 at 06:44

3 Answers3

8

The main reason for the flickering is due to the default styling of the BottomSheetDialog which defines a default animation and the dim behavior.

We can resolve the above issue by defining a custom theme using bottomSheetDialogTheme which in turn:

  1. Disables the default window animation for the BottomSheet by setting windowAnimationStyle to @null.
  2. Sets the backgroundDimEnabled attribute to false.

Example:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="bottomSheetDialogTheme">@style/AppTheme.BottomSheetDialog</item>
</style>

<style name="AppTheme.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:backgroundDimEnabled">false</item>

    <!-- optional -->
    <item name="android:windowBackground">#99323232</item> 
</style>
blizzard
  • 5,275
  • 2
  • 34
  • 48
-1

Use Coordinate layout instead of constraint layout, and define bottomsheet layout in xml like below

    <android.support.design.widget.CoordinatorLayout 
        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">

         <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:layout_gravity="center" />

         <android.support.v4.widget.NestedScrollView 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/bottom_sheet_map"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

               <include layout="@layout/layout_bottom_sheet" />
        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>

In Java Class Use like this.

    private BottomSheetBehavior mBottomSheetBehavior;
    private View bottomSheet;
    private isBottomSheetExpand = false;
    ...
    btn1.setOnClickListner(new View.OncliView.OnClickListener(){
    @Override
        public void onClick(View v) {
            if(isBottomSheetExpand){
                openBottomSheet();
            }else{
                closeBottomSheet();
            }
        }
    });
    ...
    public void closeBottomSheet() {
       if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            isBottomSheetExpand = false;
        }
    }

    public void openBottomSheet() {
        if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            isBottomSheetExpand = true;
        }
    }
Shubham Vala
  • 1,024
  • 7
  • 18
  • This works Ok but there are problems with it. It does not have a background dim, and it's affected by software keyboard (when keyboard is shown it jumps up). – holtaf Jul 05 '18 at 09:26
  • Add this `android:windowSoftInputMode="adjustPan"` in `AndroidManifest.xml` file it will solve keyboard problem – Shubham Vala Jul 05 '18 at 12:57
  • For dim background apply `android:background="@null"` to `NestedScrollView` and give `layout_bottom_sheet` transparency color it will work. – Shubham Vala Jul 05 '18 at 13:02
  • It will throw an error "The view is not a child of CoordinatorLayout" because view is inside nested scroll view – ShadeToD Dec 29 '20 at 13:47
-1

According to documentation the setPeekHeight method is responsible for the pop up behavior. Using the

bottomSheet.setState(STATE_HIDDEN) should do the trick.
XmenR
  • 47
  • 3