Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar half width on tablet

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.

like image 243
Michał Tajchert Avatar asked Oct 01 '15 21:10

Michał Tajchert


People also ask

What layout is used for snack bar undo?

Creating A Snackbar With An Action Button In Android Studio: make(coordinatorLayout, "Message is deleted", snackbar. LENGTH_LONG) . setAction("UNDO", new View.

What is a snackbar in android?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.

How do I use snackbar in Android?

This example demonstrates how do I use snackBar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Open build.gradle (Module app) add the following dependancy -

What is an snackbar?

Snackbars provide brief feedback about an operation through a message at the bottom of the screen. Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons.

How to display a message in a snackbar?

Make a Snackbar to display a message. Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.

Can snackbars be persistent or stacked?

They should not be persistent or be stacked, as they are above other elements on screen. On Android, when an unrelated dialog or popup occurs while the snackbar is up, the snackbar timeout will reset upon the window focus being regained. This is to ensure that the user will be able to read the snackbar for the full intended duration.


2 Answers

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

enter image description here

like image 59
Rahul Tiwari Avatar answered Oct 17 '22 13:10

Rahul Tiwari


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);
like image 4
Oleg Novosad Avatar answered Oct 17 '22 12:10

Oleg Novosad