Saturday, February 24, 2018

android kotlin - Snackbar example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.graphics.Color
import android.graphics.Typeface
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.widget.Button
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /*
            *** reference source developer.android.com ***

            Snackbar make (View view, CharSequence text, int duration)
                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.

                Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable
                certain features, such as swipe-to-dismiss and automatically
                moving of widgets like FloatingActionButton.

            Parameters
                view View : The view to find a parent from.
                text CharSequence : The text to show. Can be formatted text.
                duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

            Returns
                Snackbar
        */

        // Button click listener
        button_simple.setOnClickListener{
            Snackbar.make(
                    root_layout, // Parent view
                    "Hello Snackbar!", // Message to show
                    Snackbar.LENGTH_SHORT // How long to display the message.
            ).show()
        }


        // Button to show a snack bar with action enabled
        button_action.setOnClickListener{
            // Change the app background color
            root_layout.setBackgroundColor(Color.YELLOW)

            // Show a snack bar for undo option
            Snackbar.make(
                    root_layout, // Parent view
                    "We changes app background color.", // Message to show
                    Snackbar.LENGTH_LONG //
            ).setAction( // Set an action for snack bar
                    "Undo", // Action button text
                    { // Action button click listener
                        // Do something when undo action button clicked
                        root_layout.setBackgroundColor(Color.parseColor("#f2f2f2"))
            }).show() // Finally show the snack bar
        }


        // Customize the snack bar style
        button_style.setOnClickListener{
            // Initialize a new snack bar instance
            val snackbar = Snackbar.make(
                    root_layout,
                    "This is a styled snack bar.",
                    Snackbar.LENGTH_INDEFINITE
            )

            // Get the snack bar root view
            val snack_root_view = snackbar.view

            // Get the snack bar text view
            val snack_text_view = snack_root_view
                    .findViewById<TextView>(android.support.design.R.id.snackbar_text)

            // Get the snack bar action view
            val snack_action_view = snack_root_view
                    .findViewById<Button>(android.support.design.R.id.snackbar_action)

            // Change the snack bar root view background color
            snack_root_view.setBackgroundColor(Color.parseColor("#FFCFBEB4"))

            // Change the snack bar text view text color
            snack_text_view.setTextColor(Color.RED)

            // Change snack bar text view text style
            snack_text_view.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC)

            // Change the snack bar action button text color
            snack_action_view.setTextColor(Color.YELLOW)

            // Set an action for snack bar
            snackbar.setAction("Hide Me",{
                // Hide the snack bar
                snackbar.dismiss()
            })

            // Finally, display the snack bar
            snackbar.show()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    >
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_new"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="top|center_horizontal"
        >
        <Button
            android:id="@+id/button_simple"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Simple Snackbar"
            android:textAllCaps="false"
            />
        <Button
            android:id="@+id/button_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Snackbar Action"
            android:textAllCaps="false"
            />
        <Button
            android:id="@+id/button_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Snackbar Style"
            app:layout_anchor="@id/button_simple"
            android:textAllCaps="false"
            />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>