1

So I want to change the vertical padding on Android's SnackBar in a clean maintainable way.

I tried to use:

  • setPadding and setMargin on the FrameLayout, LinearLayout and TextView used to create the SnackBar

  • includeFontPadding, setLineSpacing and setSingleLineon the TextView

But nothing seemed to work.

However, I found this answer:

In dimens.xml.

Use this:

<dimen name="design_snackbar_padding_horizontal">0dp</dimen> But remember that this will get applied to all the snackbars in your application.

Which does work, but the ugly way, it's applied to all the SnackBars in the app and it's not safe since I'm overriding a private property that maybe renamed or removed in the future.

I'm trying to create something similar and Facebook Messenger's (previously) and Youtube's (Funny enough, Google) internet connection indicator:

Snackbar.make(container, message, duration).apply {

    val snackBarView = view.apply {
        setBackgroundColor(ContextCompat.getColor(this@MainActivity, connectivity.color))
    }

    (snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView).apply {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            textAlignment = View.TEXT_ALIGNMENT_CENTER
        else
            gravity = Gravity.CENTER
    }

}.show()

So this's what i'm expecting and am getting using the answer provided above:

Expected

And this's what i'm actually getting:

Actual

Thanks in advance!

Ahmed Mourad
  • 193
  • 1
  • 2
  • 18
  • You are just changing the Gravity of the text, but from what I understand is you want to reduce the height of the `SnackBar`. To do that what I would do is change the layout parms of the the `TextView` that you got from the `SnackBar`. – Saran Sankaran Jul 22 '19 at 07:27
  • I did try that, i tried creating a new `LinearLayout.LayoutParams` with `WRAP_CONTENT` for the height but it didn't work. The extra space is actually padding and not height, it can be overriden using: ` 4dp ` – Ahmed Mourad Jul 22 '19 at 07:36
  • https://github.com/ChathuraHettiarachchi/CSnackBar – Sumit Shukla Jul 22 '19 at 07:44

2 Answers2

0

Make a custom snackbar:

Snackbar snackbar = Snackbar.make(coordinatorLayout, R.string.custom_color, Snackbar.LENGTH_LONG).setAction(R.string.Ok, new View.OnClickListener() {
                  @Override
                   public void onClick(View view) {
                          Snackbar.make(coordinatorLayout, R.string.click_action, Snackbar.LENGTH_SHORT).show();
                   }
         }).setActionTextColor(Color.RED);
            View snackview = snackbar.getView();
            TextView txtView = snackview.findViewById(R.id.snackbar_text);
            txtView.setPadding(0,0,0,0);
            snackbar.show();
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
0

The provided answer didn't work out for me. So after a lot of trial, I finally solved my problem (which is exactly the same as described above). To reduce the vertical padding in snackbar, try this:

        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    llp.topMargin = -30;
    llp.bottomMargin = -30;
    snackText.setLayoutParams(llp);

Below is the full code I wrote to make functionality like the youtube app for checking and showing internet errors.

    public void showNoInternet(){
    noInternetSnack = Snackbar.make(noInternetSnackParent, "No internet connection", Snackbar.LENGTH_INDEFINITE)
            .setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
            .setBackgroundTint(PaletteUtils.getSolidColor(PaletteUtils.MATERIAL_RED));

    Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) noInternetSnack.getView();
    TextView snackText = snackbarLayout.findViewById(com.google.android.material.R.id.snackbar_text);
    snackText.setTextSize(getResources().getDimension(R.dimen.newsMoreTextSize)/3);
    snackText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    llp.topMargin = -30;
    llp.bottomMargin = -30;
    snackText.setLayoutParams(llp);

    noInternetSnack.show();
}

public void hideNoInternet(){
    if (noInternetSnack != null && noInternetSnack.isShown()){
        Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) noInternetSnack.getView();
        noInternetSnack.setBackgroundTint(PaletteUtils.getSolidColor(PaletteUtils.MATERIAL_GREEN));
        TextView snackText = snackbarLayout.findViewById(com.google.android.material.R.id.snackbar_text);
        snackText.setText("You are back online");
        new java.util.Timer().schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        noInternetSnack.dismiss();
                    }
                },
                500
        );
    }
}

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        NetworkInfo currentNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        if (currentNetworkInfo.isConnected()) {
            hideNoInternet();
        } else {
            showNoInternet();
        }
    }
};
Dharman
  • 30,962
  • 25
  • 85
  • 135