Canato
Canato

Reputation: 3978

Snackbar cover my Button View on Constraint Layout

I know that by default only the floating button change his position when a wild Snackbar appears.

But if we look this question (really close to mine) we can found a solution here.

Based on this I'm hopping that we have a similar or better solution for Coordinator Layout.

What is happen:

My save button got cover when snackbar appears. (sorry resize image in stack with s.png is not working)

Save Button Snackbar cover save button

What I wanna:

My Button or the whole Constraint View go up when Snackbar appears

My Code:

xml code

<android.support.constraint.ConstraintLayout
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="wrap_content"
android:background="@drawable/background_white_round">


<TextView
    android:id="@+id/dialog_title_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="24dp"
    android:text="@string/str_title"
    android:textColor="@color/solito_text_black"
    android:textSize="@dimen/size_text_title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/dialog_description_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="20dp"
    android:text="@string/str_loading"
    android:textColor="@color/solito_text_grey_dark"
    android:textSize="@dimen/size_text_normal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/dialog_title_text" />

<Button
    android:id="@+id/dialog_ok_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="24dp"
    android:background="@color/background_transparent"
    android:text="@android:string/ok"
    android:textColor="@color/solito_button_purple"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/dialog_description_text"/>

<Button
    android:id="@+id/dialog_cancel_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="24dp"
    android:background="@color/background_transparent"
    android:text="@string/str_cancel"
    android:textColor="@color/solito_button_purple"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@id/dialog_ok_btn"
    app:layout_constraintTop_toBottomOf="@id/dialog_description_text"/>
</android.support.constraint.ConstraintLayout>

java code

private void showSnackBarUndo() {
    //Snack Bar to Undo
    Snackbar snackbar = Snackbar
            .make(Objects.requireNonNull(getView()), getResources().getString(R.string.str_list_cleaned), Snackbar.LENGTH_LONG)
            .setAction(getResources().getString(R.string.str_undo), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DataStorePersistence.getInstance().setAddressArrayListTemp(getContext(), listTitle, listID, addressArrayListUndo);
                    updateListContent();
                    Snackbar
                            .make(getView(),
                                        getResources().getString(R.string.str_list_restored),
                                    Snackbar.LENGTH_SHORT)
                            .show();
                }
            });
    snackbar.show();
}

EDIT: I tried a lot of stuffs. Example try to put behavior in one class button custom, but didn't work. Studying the Material Design Android don't look like they wanna let you put your button in the bottom of the screen. So By now I'm changing to a Floating Button. Is not a solution for the question, but maybe help someone who gets here.

Upvotes: 1

Views: 1954

Answers (3)

tingyik90
tingyik90

Reputation: 1711

Not sure if my SnackProgressBar libary will help. You first create a SnackProgressBarManager, then include the button as part of the view to move with the snackBar.

// always have one instance of SnackProgressBarManager only in the activity
snackProgressBarManager = new SnackProgressBarManager(view)
        // (optional) set the view which will animate with SnackProgressBar e.g. FAB when CoordinatorLayout is not used
        .setViewToMove(buttonView)

Complete documentation is available at Github. It is very flexible if you want to use it for other purpose too.

Upvotes: 0

dominicoder
dominicoder

Reputation: 10175

Probably easiest thing to do is take the source code for FloatingActionBar.Behavior (very little if any of the code should require the view to be of type FloatingActionButton) and modify it so it works with a regular Button instead. Then just set an instance of your new custom Behavior class to your Button in your layout.

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.your.app.ButtonBehavior" />

Upvotes: 0

Thecarisma
Thecarisma

Reputation: 1433

If all method you have tried did not work. Then try increasing and decreasing the bottom margin of your save button each time the snackbar is shown and dismissed i.e

...
snackbar.addCallback(new Snackbar.Callback() {

    @Override
    public void onDismissed(Snackbar snackbar, int event) {
      LinearLayout.LayoutParams param = theSaveButton.getLayoutParams();
       param.bottomMargin = 10 ; //or the previous bottom margin
       theSaveButton.setLayoutParams(param);
    }

    @Override
    public void onShown(Snackbar snackbar) {
       LinearLayout.LayoutParams param = theSaveButton.getLayoutParams();
       param.bottomMargin = 80 ; //80dp being the height of snackbar
       theSaveButton.setLayoutParams(param);
    }
  });
snackbar.show();
...

Or at worse change the position of the button from within the snackbar listener

Upvotes: 0

Related Questions