Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any possible way to close android snackbar (LENGTH_INDEFINITE)automatically while internet connected without any action?

I have displayed android snack bar with INDEFINITE LENGTH but how to close snack bar with out any action or duration while Internet connected . I have to check internet connected or not.after internet connected snack bar will be closed automatically without any action or duration.If anybody knows kindly help me.

here is my code

public static void snack (HashMap<String,View.OnClickListener> actions,int priority,String message,Activity context){
    Snackbar B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
    if(actions!=null){
    Iterator iterator = actions.entrySet().iterator();
        B.setDuration(Snackbar.LENGTH_INDEFINITE);
    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
        iterator.remove(); // avoids a ConcurrentModificationException
    }}
    switch (priority)
    {
        case 0:
            B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
            break;
        case 1:
            B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
            break;
        case 2:
            B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
            break;
    }
    B.show();

after calling above mentioned method using the activity is as follows

If (NetworkCheck.isNetworkAvailable(this) == false) {
    MyApplication.snack(null, 0, "Network Connection failed.",class.this);
else
like image 905
RAMESH Avatar asked Sep 04 '25 02:09

RAMESH


2 Answers

I have made this singleton utility class. It kept the application class cleaner and best for future maintainability of snack bars.

public class SnackBarUtils {
    private static SnackBarUtils mInstance = null;
    private Snackbar mSnackBar;

    private SnackBarUtils() {

    }

    public static SnackBarUtils getInstance() {
        if (mInstance == null) {
            mInstance = new SnackBarUtils();
        }
        return mInstance;
    }

    public void hideSnackBar() {
        if (mSnackBar != null) {
            mSnackBar.dismiss();
        }
    }

    public void showProblemSnackBar(final Activity activity, final String message) {
        mSnackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);
        // Changing action button text color
        View sbView = mSnackBar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        mSnackBar.show();
    }
}
like image 181
Farhan Avatar answered Sep 06 '25 17:09

Farhan


You can make changes in you code as follows:

   Snackbar B; //make it as global

    public static void snack (HashMap<String,View.OnClickListener> actions,int priority,String message,Activity context){
      B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
      if(actions!=null){
      Iterator iterator = actions.entrySet().iterator();
        B.setDuration(Snackbar.LENGTH_INDEFINITE);
      while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
        iterator.remove(); // avoids a ConcurrentModificationException
      }}
      switch (priority){
        case 0:
                B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
            break;
        case 1:
            B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
            break;
        case 2:
            B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
            break;
    }
    B.show();

Make another method for hiding snackbar as

private static hideSnackbar(){
   if(B !=null && B.isShown()){
           B.dismiss();
   }
}

And in your condition

If (NetworkCheck.isNetworkAvailable(this) == false) {
    MyApplication.snack(null, 0, "Network Connection failed.",class.this)
}else{
    MyApplication.hideSnackbar();
}
like image 34
Pankaj Avatar answered Sep 06 '25 16:09

Pankaj