Android Persist A SnackBar Via Actions

Situation: A user is logged in, you show a snack bar that says they are successfully logged in, then move to another intent, but the problem is that when navigating the snackbar, it is canceled / destroyed. how we keep it in all activities like toast no matter what activity you go to ... it stays alive and well and follows that timeout.

+3


source to share


3 answers


You can make your own snack bar-like toast:

custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toast_layout"
    android:layout_gravity="bottom|center_horizontal"
    android:background="#545454"
    android:gravity="left|fill_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/toast_text"
        android:layout_gravity="bottom"
        android:textColor="#ffffff"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="8dp" />
</LinearLayout>

      

and show it like this:



public void showCustomToast(String msg)
{
    //inflate the custom toast
    LayoutInflater inflater = getLayoutInflater();
    // Inflate the Layout
    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.toast_layout));

    TextView text = (TextView) layout.findViewById(R.id.toast_text);

    // Set the Text to show in TextView
    text.setText(msg);

    Toast toast = new Toast(getApplicationContext());

    //Setting up toast position, similar to Snackbar
    toast.setGravity(Gravity.BOTTOM | Gravity.LEFT | Gravity.FILL_HORIZONTAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show(); 
}

      

if you get ERROR / AndroidRuntime (5176): Caused by: java.lang.RuntimeException: Cannot create a handler inside a thread that did not call Looper.prepare ()

wrap the code inside the showCustomToast function inside this launch fuction,

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {

    }

});

      

+2


source


Make a wrap around your view group and add it to each of your layouts. You can also use a window overlay. See here



0


source


In fact, SnackBars don't have to be persistent or complex as they are taller than other items on the screen. You can see this rule in Google Design

But you can use 3rd party Material Design library here: rey5137 / material

You can show it by doing one of the following functions:

public void show(Activity activity);

//parent should be FrameLayout or RelativeLayout so SnackBar can algin bottom.
public void show(ViewGroup parent); 

// It only work if SnackBar is already attached to a parent view.
public void show(); 

      

0


source







All Articles