Android Design Support Library uses Snackbar

Keywords: Android xml encoding

Snackbar provides lightweight feedback on operations. It can display a short message from the bottom of the screen, similar to Toast, but it is more flexible and powerful.  
When it is displayed for a period of time or when the user interacts with the screen, it will disappear automatically.
It can include an action;
Snackbar appears on all other elements of the screen, with only one Snackbar showing at the same time.

We just need to add references to AS:

compile 'com.android.support:design:23.3.0'


Using 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:onClick="createSnackbar"
        android:text="snackba test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>
MainActivity

package com.example.administrator.mysnackbar;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   private CoordinatorLayout container;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = (CoordinatorLayout) findViewById(R.id.container);
    }

    public static void setSnackbarMessageTextColor(Snackbar snackbar, int color) {
        View view = snackbar.getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(color);
    }

    public void createSnackbar(View v) {
            Snackbar snackbar= Snackbar.make(container,"SnackbarTest", Snackbar.LENGTH_LONG).setAction("Action", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Snackbar.make(container,"ActionClick",Snackbar.LENGTH_LONG).show();
                }
            });
            setSnackbarMessageTextColor(snackbar, Color.parseColor("#ff6699"));
            snackbar.show();
    }

}


But how to set the color of Snackbar's message text and Action text?

Actions can be set through Snack's bar's public API Isnackbar. setAction TextColor (int color), but it's not easy to use, but there's no API to set the message text color, but a way to look at Snack bar. class is found:

 public View getView() {
        return this.mView;
 }
The mView returned by getView() is the SnackbarLayout instance, and the layout of this class is eventually layout_snackbar_include.xml.

So we can write the following help method to set the color of the message text.

 public static void setSnackbarMessageTextColor(Snackbar snackbar, int color) {
        View view = snackbar.getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(color);
}

Source download Give a compliment to a friend you like.


Posted by bprof on Thu, 14 Feb 2019 13:30:18 -0800