[Android] Add a util method for snackbar with cancel button

Geoffrey Métais git at videolan.org
Mon Jan 4 16:50:53 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan  4 16:25:44 2016 +0100| [8154d2c5cd1879c8f51f6ab67f7687da6b72ff88] | committer: Geoffrey Métais

Add a util method for snackbar with cancel button

> https://code.videolan.org/videolan/vlc-android/commit/8154d2c5cd1879c8f51f6ab67f7687da6b72ff88
---

 .../src/org/videolan/vlc/gui/helpers/UiTools.java     | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
index f225e23..902fd5a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -26,8 +26,11 @@ package org.videolan.vlc.gui.helpers;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.TypedArray;
+import android.os.Handler;
+import android.os.Looper;
 import android.preference.PreferenceManager;
 import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.support.design.widget.Snackbar;
 import android.text.TextUtils;
 import android.util.DisplayMetrics;
@@ -43,6 +46,9 @@ public class UiTools {
 
     private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
 
+    private static final Handler sHandler = new Handler(Looper.getMainLooper());
+    public static final int DELETE_DURATION = 3000;
+
     /** Print an on-screen message to alert the user */
     public static void snacker(@NonNull View view, @NonNull int stringId) {
         Snackbar.make(view, stringId, Snackbar.LENGTH_SHORT).show();
@@ -53,6 +59,19 @@ public class UiTools {
         Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
     }
 
+    /** Print an on-screen message to alert the user, with undo action */
+    public static void snackerWithCancel(@NonNull View view, @NonNull String message, @NonNull final Runnable action) {
+        Snackbar.make(view, message, DELETE_DURATION)
+                .setAction(android.R.string.cancel, new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        sHandler.removeCallbacks(action);
+                    }
+                })
+                .show();
+        sHandler.postDelayed(action, DELETE_DURATION);
+    }
+
     public static int convertPxToDp(int px) {
         DisplayMetrics metrics = VLCApplication.getAppResources().getDisplayMetrics();
         float logicalDensity = metrics.density;



More information about the Android mailing list