5

How can i customize my Snackbar layout like on Google Drive as shown below?

Google drive Snackbar

EDIT: Is this the only way around? how to customize snackBar's layout?

1
  • 1
    Perhaps that is not a Snackbar. Commented Dec 25, 2016 at 0:47

2 Answers 2

2

I've managed to work it out with Mike and JJD anwser here. Here's my code

At my activity:

private void showLoggedUser() {
    View view = findViewById(android.R.id.content);
    Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    layout.findViewById(android.support.design.R.id.snackbar_text).setVisibility(View.INVISIBLE);

    FirebaseUser currentUser = FirebaseUtils.getCurrentUser();
    View snackView = mInflater.inflate(R.layout.login_snackbar, null);
    ((TextView)snackView.findViewById(R.id.user_name)).setText(currentUser.getDisplayName());
    ((TextView)snackView.findViewById(R.id.user_mail)).setText(currentUser.getEmail());

    final ImageView imageview = (ImageView) snackView.findViewById(R.id.user_ic);
    ImageDownloader imageDownloader = new ImageDownloader() {
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            RoundedBitmapDrawable bmDrawable;
            bmDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
            bmDrawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
            imageview.setImageDrawable(bmDrawable);
        }
    };
    imageDownloader.execute(currentUser.getPhotoUrl().toString());

    layout.addView(snackView, 0);
    snackbar.show();
}

My layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent" android:gravity="center_vertical"
    android:paddingTop="@dimen/activity_margin" android:paddingBottom="@dimen/activity_margin">

    <ImageView
        android:id="@+id/user_ic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_user_white"
        android:adjustViewBounds="false" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="10dp">

        <TextView
            android:text="Username"
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Subtitle"
            android:textColor="@android:color/white" />

        <TextView
            android:text="Usermail"
            android:id="@+id/user_mail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Caption"
            android:textColor="@android:color/white" />

    </LinearLayout>

Final result final result

1

Why would this need to be a snackbar? Just add a view at the bottom of the layout that holds the information you need.

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/black"
    android:layout_alignParentStart="true">

    <ImageView
        android:id="@+id/image"
        android:src="@drawable/image"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

    <TextView
        android:id="@+id/email"
        android:textSize="25sp"
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@+id/image" />

    <TextView
        android:id="@+id/name"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="Name"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/image" />
</RelativeLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.