Custom Snackbar width is not setting MATCH_PARENT in Tablets

Viewed 4680

Snackbar width is matching to full width in mobiles but its not setting match_parent when running in tablet. Kindly help me!Code is below

final Snackbar mSnackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) mSnackbar.getView();
layout.setPadding(0,0,0,0);
TextView textView = (TextView) 
layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);
snackView = inflater.inflate(R.layout.snackbar_layout, null);
layout.addView(snackView, 0);
4 Answers

Use the following code snippet to support full width in tablet landscape mode:

Snackbar snackbar = Snackbar.make(view, "Sample Text", Snackbar.LENGTH_LONG)
        .setAction("Sample Text", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
(snackbar.getView()).getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
snackbar.show();

If you use this theme:

"Theme.MaterialComponents"

the snackbar has margin and rounded corner.

To remove margin:

View viewInSnk = snkbr.getView();
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) snkbr.getView().getLayoutParams();
params.setMargins(0, 0, 0, 0);
snkbr.getView().setLayoutParams(params);

and to remove round corner:

viewInSnk.setBackgroundDrawable(getResources().getDrawable(R.drawable.snackbar_shape));

and for shape :

<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#313131"/>
    <corners android:radius="0dp"/>
</shape>
Related