Monday, May 30, 2016

android - Snackbar custom view example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/cLayout"
    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"
    tools:context=".MainActivity"
    >
    <RelativeLayout
        android:id="@+id/rl"
        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"
        android:background="#909baf"
        >
        <Button
            android:id="@+id/btn_task"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Snackbar"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private Button mButtonTask;

    private Snackbar mSnackbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mButtonTask = (Button) findViewById(R.id.btn_task);

        // Initialize an empty text Snackbar
        mSnackbar = Snackbar.make(mRelativeLayout,"",Snackbar.LENGTH_INDEFINITE);

        // Get the Snackbar view
        View view = mSnackbar.getView();

        // Set the background drawable for Snackbar
        view.setBackground(ContextCompat.getDrawable(mContext,R.drawable.view_top_border));

        // Set the Snackbar action button text color
        mSnackbar.setActionTextColor(Color.RED);

        // Get the Snackbar action text instance
        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);

        // Change the Snackbar action text color
        tv.setTextColor(Color.parseColor("#365A31"));

        // Italic the Snackbar action text
        tv.setTypeface(tv.getTypeface(),Typeface.ITALIC);

        // Set a click listener for task button
        mButtonTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Change the Snackbar text
                mSnackbar.setText("A sample Snackbar.");

                // Set an action for Snackbar
                // Set a click listener for Snackbar action button
                mSnackbar.setAction("CONFIRM", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Do something when Snackbar action button clicked
                    }
                });

                // Display the Snackbar
                mSnackbar.show();
            }
        });
    }
}
res/drawable/view_top_border.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#cde2bb"/>
            <stroke android:color="#ff5acd" android:width="2dp"/>
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#cde2bb"/>
        </shape>
    </item>
</layer-list>