Snackbar half width on tablet
Asked Answered
R

5

8

I have problem with setting Snackbar on tablet (API 22), on phones (API 23 and 22) it works fine (from edge to edge), even when horizontal. Result is such Snackbar as below:enter image description here

FloatingActionButton (support library) also doesn't move (when on phone it does).

My layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/snackParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/margin_fab"
        android:layout_marginRight="@dimen/margin_fab"
        app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>

And using in MainActivity

private void showMessage(String msg) {
    if(snackParent != null) {
        snackbar = Snackbar.make(snackParent, msg, Snackbar.LENGTH_SHORT);
        snackbar.setAction("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbar.dismiss();
            }
        });
        snackbar.show();
    }
}

I have only other resources file with dimensions (fab margin) for tablets (w820dp), styles folder is same between phone and tablet. I have also tried invalidating Android Studio cache. I use com.android.support:design:23.0.1 targetSdkVersion=23 and compileSdkVersion=23, buildToolsVersion=23.0.1.

Rangy answered 1/10, 2015 at 21:2 Comment(3)
I think this is the default behavior of snackbar on tablets. check this outStephanus
Yeah, I just tested on empty project with older support library versions, targetSdkVersion etc. and all seems to point it is on purpose. Can you post your replay as answer?Flavius
This might be a bit late but the following works for me : mSnackbar.getView().getLayoutParams().width = AppBarLayout.LayoutParams.MATCH_PARENT;Mediocrity
S
8

I think this is the default behavior of snackbar on tablets. check this out .

enter image description here

Stephanus answered 1/10, 2015 at 21:26 Comment(0)
P
6

You can do whatever you want with your snackbar. We had the same issue with tablets and we solved like this (e.g. code below is positioning snackbar at top and gives it the full width of the parent)

View view = snackbar.getView();

// Position snackbar at top
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
view.setLayoutParams(params);
Parameter answered 16/5, 2018 at 10:48 Comment(1)
If I use view as above instead of coordinatorLayout, I get CoordinatorLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams errorCancer
H
1

After digging into code from github. I found one workaround. You just need to add below lines into styles.xml. Change the width as per your requirement

<style name="Widget.Design.Snackbar" parent="android:Widget">
    <item name="maxActionInlineWidth">500dp</item>
</style>

If you want to change number of lines of default textview of Snackbar, you can change following style.

<style name="Widget.MaterialComponents.Snackbar.TextView" parent="Widget.AppCompat.TextView">
    <item name="android:maxLines">3</item>
</style>
Hawthorne answered 23/2, 2023 at 12:41 Comment(0)
B
0

ViewGroup.LayoutParams layoutParams = snackbarView.getLayoutParams();

        if (layoutParams instanceof FrameLayout.LayoutParams) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutParams;
            params.gravity = Gravity.TOP;
            params.width = FrameLayout.LayoutParams.MATCH_PARENT;
            snackbarView.setLayoutParams(params);
        } else if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layoutParams;
            params.gravity = Gravity.TOP;
            params.width = FrameLayout.LayoutParams.MATCH_PARENT;
            snackbarView.setLayoutParams(params);
        } else {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutParams;
            params.gravity = Gravity.TOP;
            params.width = FrameLayout.LayoutParams.MATCH_PARENT;
            snackbarView.setLayoutParams(params);
        }
Boleyn answered 13/10, 2020 at 10:28 Comment(0)
E
0

I couldn't able to solve this issue by using above approach. After looking into the source code

I added below code into my styles.xml

<style name="Widget.Design.Snackbar" parent="android:Widget">
    <item name="android:maxWidth">-1px</item>
</style>

currently it is set to

<item name="android:maxWidth">@dimen/design_snackbar_max_width</item>
<dimen name="design_snackbar_max_width">576dp</dimen>
Enid answered 20/4, 2023 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.