0

I'm trying to show a custom Snackbar as in this example:

how to customize snackBar's layout?

This is my custom code to create a Snackbar:

protected void showSnackBar(View view) {

    Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
    textView.setVisibility(View.INVISIBLE);

    LayoutInflater inflater = LayoutInflater.from(this);

    View snackView = inflater.inflate(R.layout.custom_snackbar, null);

    layout.addView(snackView, 0);

    ViewGroup group = (ViewGroup) snackbar.getView();
    group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));

    snackbar.show();

}

My goal is to have a method in my base activity to call from any other activity.

But my problem is when I show the Snackbar, that it is shown under the keyboard:

Example

In addition it does not show all views of layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativelayout_sync_schedule_runs"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/green_wasabi"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal|bottom">

<ProgressBar
    android:id="@+id/progressbar_infinite"
    android:layout_width="30dp"
    android:layout_height="30dp"
    style="@android:style/Widget.Material.ProgressBar.Small"
    android:layout_centerVertical="true"
    android:layout_marginRight="15dp"
    android:layout_toLeftOf="@+id/textview_sync_runs"
    android:layout_toStartOf="@+id/textview_sync_runs" />


<com.runator.ui.widget.text.CustomTextView
    android:id="@+id/textview_sync_runs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/import_runs"
    android:textColor="@color/black_midnight"
    style="@style/texts.medium_medium_big"
    app:font_type="bold"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

If anyone has any suggestions I will be happy to hear it.

Thanks in advance.

Community
  • 1
  • 1
dpulgarin
  • 556
  • 5
  • 17
  • Can you share your layout.xml file that belongs to BaseActivity ? – Olkunmustafa Apr 07 '16 at 14:31
  • My BaseActivity not have layout – dpulgarin Apr 07 '16 at 14:32
  • It seems you are using a translucent navigation bar (either set in your styles/theme or programatically). That is why your layout stretches to the full height of the display and your snackbar is appended at the bottom. You can try adding android:fitsSystemWindows="true" to your parent element in your layout, but then the content will not stretch behind the navigation bar. If you still want to have the content to stretch behind it but have the snacker on top, you will have to programatically change its position when it is shown. – peshkira Apr 07 '16 at 14:38

3 Answers3

3

First of all, you don't need to set background colour programmatically, as you are already doing that in xml files, so remove these lines

ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));

Secondly, try with passing Coordinator Layout of the activity as the argument, instead of Linear/Relative Layout, i.e. add coordinator layout as the parent layout in the activity and pass it.

Check the layout bounds of the coordinator layout in the xml view. It should not extend the bounds of activity.

Yash
  • 5,225
  • 4
  • 32
  • 65
2

Can you try that code for adding the Snackbar on your Baseactivity.

Snackbar snackbar = Snackbar.make( rootView, text, Snackbar.LENGTH_LONG );
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor( this.mContext.getResources().getColor( R.color.pepapp_bright_red ) );
snackbar.show();
Olkunmustafa
  • 3,103
  • 9
  • 42
  • 55
1

Code below works same as Snackbar, but full of customized (UI, animation, actions...) components.

public class CustomSnackBar extends 
        BaseTransientBottomBar<CustomSnackBar> {

    /**
     * Time duration till the snackbar is visible to user
     */
    public static final int DURATION_SEC_2 = 2000;

    /**
     * Constructor for the transient bottom bar.
     *
     * @param parent The parent for this transient bottom bar.
     * @param content The content view for this transient bottom bar.
     * @param callback The content view callback for this transient bottom bar.
     */
    private CustomSnackBar(ViewGroup parent, View content, ContentViewCallback callback) {
        super(parent, content, callback);
    }

    public static CustomSnackBar make(@NonNull ViewGroup parent, @Duration int duration) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        final View content = inflater.inflate(R.layout.snackbar_view, parent, false);
        final ContentViewCallback viewCallback = new ContentViewCallback(content);
        final CustomSnackBar customSnackbar = new CustomSnackBar(parent, content, viewCallback);
        customSnackbar.setDuration(duration);
        return customSnackbar;
    }

    public CustomSnackBar setText(CharSequence text) {
        TextView textView = (TextView) getView().findViewById(R.id.snackbar_text);
        textView.setText(text);
        return this;
    }

    public CustomSnackBar setAction(CharSequence text, final View.OnClickListener listener) {
        Button actionView = (Button) getView().findViewById(R.id.snackbar_action);
        actionView.setText(text);
        actionView.setVisibility(View.VISIBLE);
        actionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
                // Now dismiss the Snackbar
                dismiss();
            }
        });
        return this;
    }

    private static class ContentViewCallback implements BaseTransientBottomBar.ContentViewCallback {

        private View content;

        public ContentViewCallback(View content) {
            this.content = content;
        }

        @Override
        public void animateContentIn(int delay, int duration) {
            ViewCompat.setScaleY(content, 0f);
            ViewCompat.animate(content).scaleY(1f).setDuration(duration).setStartDelay(delay);
        }

        @Override
        public void animateContentOut(int delay, int duration) {
            ViewCompat.setScaleY(content, 1f);
            ViewCompat.animate(content).scaleY(0f).setDuration(duration).setStartDelay(delay);
        }
    }
}
wscourge
  • 10,657
  • 14
  • 59
  • 80