Monday, May 30, 2016

android - How to set an action for Snackbar

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context=".MainActivity"
    android:background="#f2fff1"
    >
    <Button
        android:id="@+id/btn_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide This Button"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

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);

        // Set a click listener for task button
        mButtonTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Hide the button widget itself
                mButtonTask.setVisibility(View.GONE);

                // Change the Snackbar text
                mSnackbar.setText("Button widget now hidden.");

                /*
                    setAction
                        Snackbar setAction (int resId, View.OnClickListener listener)

                        Set the action to be displayed in this Snackbar.

                    Parameters
                        resId int: String resource to display
                        listener : View.OnClickListener: callback to be invoked when the action is clicked
                    Returns
                        Snackbar
                */

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

                // Display the Snackbar
                mSnackbar.show();
            }
        });
    }
}