question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Snackbar] Can't change top and bottom padding of snackbar's text view

See original GitHub issue

Description: Setting <item name="android:padding">0dp</item> changes the start and end padding but the top and bottom remain unchanged. Setting android:paddingTop and android:paddingBottom has the same effect.

qemu-system-x86_64_QcZZwlDmeT
From the layout inspector studio64_w5ajHNfAxV

Expected behavior: The top and bottom padding change, similar to start and end.

Source code:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="snackbarTextViewStyle">@style/AppTheme.Snackbar.TextView</item>
</style>

<style name="AppTheme.Snackbar.TextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
    <item name="android:padding">0dp</item>
</style>

Android API version: 29

Material Library version: 1.2.0-alpha05

Device: Android Emulator - Pixel 2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
DanielMartinuscommented, May 27, 2020

I just went through the source and found a way of updating the padding of the Snackbar. I’ll explain the steps that I went through, see tl;dr at the end.

Snackbar.java

In Snackbar.java on line167 you can see it inflating one of two layouts: R.layout.mtrl_layout_snackbar_include : R.layout.design_layout_snackbar_include

In those layouts you see almost two similar layouts, just different stylings and references. What they have in common though is the TextView and Button. The TextView is named: android:id=“@+id/snackbar_text”

However this snackbar_text isn’t used anywhere in snackbar.java but after some more investigation I found it’s used in SnackbarContentLayout referenced all over the place, but most importantly on line 163 in snackbar.java.

final SnackbarContentLayout content =
        (SnackbarContentLayout)
            inflater.inflate(
                hasSnackbarContentStyleAttrs(parent.getContext())
                    ? R.layout.mtrl_layout_snackbar_include
                    : R.layout.design_layout_snackbar_include,
                parent,
                false);

SnackbarContentLayout

Going into this SnackbarContentLayout I looked for snackbar_text which is assigned to messageView. Following this messageView you quickly find line124 where it’s doing a condition if the textview contains a single or multilines updating the bottom and top padding.

Following back where updateViewsWithinLayout is called from (line101) you see it’s choosing between a multiLineVPadding or singleLineVPadding.

Following multiLineVPadding you’ll see it assigned on line91

final int multiLineVPadding =
        getResources().getDimensionPixelSize(R.dimen.design_snackbar_padding_vertical_2lines);

Finally looking up design_snackbar_padding_vertical_2lines you’ll see it has 24dp assigned.

Tl;dr

The padding of the Snackbar varies between singleline (14dp) and multiline (24dp). You need to somehow override this. This can be done by overriding this value in your dimens.xml for example.

<dimen name="design_snackbar_padding_vertical_2lines">14dp</dimen>
3reactions
hunterstichcommented, Apr 6, 2020

You can set your theme’s snackbarStyle with android:paddingTop and android:paddingBottom to achieve increase the padding.

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="snackbarStyle">@style/Widget.MyApp.Snackbar</item>
</style>

<style name="Widget.MyApp.Snackbar" parent="@style/Widget.MaterialComponents.Snackbar">
    <item name="android:layout_margin">45dp</item>
    <item name="android:padding">32dp</item>
</style>

image

Are you trying to remove all top and bottom padding?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add margins to Snackbar view - android - Stack Overflow
By adding padding or margins to your CoordinatorLayout you can control the position and move the Snackbar from the bottom of the screen....
Read more >
Snackbars - Material Design
Anchoring a snackbar​​ By default, Snackbars will be anchored to the bottom edge of their parent view. However, you can use the setAnchorView...
Read more >
How To Create a Snackbar / Toast - W3Schools
Snackbar / Toast. Snackbars are often used as tooltips/popups to show a message at the bottom of the screen. Click on the button...
Read more >
Android Snackbar Example Tutorial - DigitalOcean
A code snippet to display a basic android Snackbar is shown below. ... No changes in the activity_main.xml code which contains the ...
Read more >
Configuring Snackbar in Jetpack Compose when using ...
I would like to show you one approach you could take in setting up snackbars for your Android application using Scaffold. This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found