Search code examples
androidtabletandroid-snackbarsnackbar

Android snackbar action not right aligned on tablets


Note: I've seen the existing question titled "Align Snackbar's Action on the right", however this does not address my particular question.

According to the material design spec the action in a snackbar should be right/end aligned. The default behaviour on smaller screens seems to work as expected (full width of screen, action is aligned to the right of the screen), but on tablets I'm seeing the action immediately next to the description/title text with some padding. Example here:

Snackbar action position example

I've seen this on a real Nexus 9 (api 25), and 3 emulators (api 22, 23, and 24) in both portrait and landscape.

The layout xml for the snackbar contents in the Android source (design_layout_snackbar_include.xml) seems to suggest the action button should be right/end aligned so I'm a bit baffled as to why I'm not seeing this.

I first saw this occurring in one of the apps my company is developing, and I've now made a test app to reproduce the issue (which I used to produce the example image linked earlier). All the app does is display "Hello World!" in an activity, and when the back button is pressed a snackbar is shown to confirm the action. My test activity looks like this:

package au.com.test.snackbartest;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
    }

    @Override
    public void onBackPressed()
    {
        Snackbar snackbar = Snackbar.make( findViewById( R.id.activity_main ), "Exit?", Snackbar.LENGTH_LONG );

        snackbar.setAction( "EXIT", new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                finish();
            }
        } );

        snackbar.show();
    }
}

The layout xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="au.com.test.snackbartest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

</RelativeLayout>

Using a coordinator layout instead of relative layout doesn't affect this behaviour. Also, extending Activity instead of AppCompatActivity makes no difference either.

So my question is, am I using the snackbar wrong somehow, is this a bug in Android, or is the design spec simply left up to the developer to fully implement with a custom snackbar layout?

Any help or clarification on this would be greatly appreciated!


Solution

  • This is a bug introduced with the 25.1.0 support library. Reverting back to 25.0.1 results in the correct behaviour. Turns out a bug report had already been filed here:

    Issue 231850: Snackbar Action Text Not Right-Aligned in 25.1.0

    Thanks ianhanniballake for the suggestion of it being a regression in the support library update.