From 990a35c47500c6a3f4292d41450a5f6cb689c7b1 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Wed, 11 Jan 2017 14:47:25 +0100 Subject: Custom toast view added --- art/ic_toast.svg | 229 +++++++++++++++++++ art/ic_toast_error.svg | 247 +++++++++++++++++++++ art/render.rb | 2 + .../utils/ui/ConversationsPlusToast.java | 101 +++++++++ src/main/res/drawable-hdpi/ic_toast.png | Bin 0 -> 1708 bytes src/main/res/drawable-hdpi/ic_toast_error.png | Bin 0 -> 1764 bytes src/main/res/layout/cplus_toast_container.xml | 22 ++ 7 files changed, 601 insertions(+) create mode 100644 art/ic_toast.svg create mode 100644 art/ic_toast_error.svg create mode 100644 src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java create mode 100644 src/main/res/drawable-hdpi/ic_toast.png create mode 100644 src/main/res/drawable-hdpi/ic_toast_error.png create mode 100644 src/main/res/layout/cplus_toast_container.xml diff --git a/art/ic_toast.svg b/art/ic_toast.svg new file mode 100644 index 00000000..7bf2c515 --- /dev/null +++ b/art/ic_toast.svg @@ -0,0 +1,229 @@ + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/art/ic_toast_error.svg b/art/ic_toast_error.svg new file mode 100644 index 00000000..7f6e8c1e --- /dev/null +++ b/art/ic_toast_error.svg @@ -0,0 +1,247 @@ + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + ! + diff --git a/art/render.rb b/art/render.rb index b4f84769..1520e958 100755 --- a/art/render.rb +++ b/art/render.rb @@ -13,6 +13,8 @@ resolutions = { images = { 'conversations_baloon.svg' => ['ic_launcher', 48], 'conversations_mono.svg' => ['ic_notification', 24], + 'ic_toast.svg' => ['ic_toast', 24], + 'ic_toast_error.svg' => ['ic_toast_error', 24], 'ic_received_indicator.svg' => ['ic_received_indicator', 12], 'ic_send_text_offline.svg' => ['ic_send_text_offline', 36], 'ic_send_text_online.svg' => ['ic_send_text_online', 36], diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java new file mode 100644 index 00000000..3576d1cd --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java @@ -0,0 +1,101 @@ +package de.thedevstack.conversationsplus.utils.ui; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.StringRes; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ImageView; +import android.widget.Toast; + +import de.thedevstack.conversationsplus.ConversationsPlusApplication; +import de.thedevstack.conversationsplus.R; + +/** + * Wrapper for custom styled Toasts for Conversations+. + */ +public final class ConversationsPlusToast { + + /** + * Creates an error toast with text and duration + * @param text + * @param duration + */ + public static void makeErrorToast(final CharSequence text, final int duration) { + Handler toastHandler = new Handler(Looper.getMainLooper()); + toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration, true)); + } + + /** + * Creates an error toast with text from resource and duration + * @param resId + * @param duration + */ + public static void makeErrorToast(@StringRes int resId, int duration) { + makeErrorToast(ConversationsPlusApplication.getAppContext().getString(resId), duration); + } + + /** + * Creates an toast with text and duration + * @param text + * @param duration + */ + public static void makeToast(final CharSequence text, final int duration) { + Handler toastHandler = new Handler(Looper.getMainLooper()); + toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration)); + } + + /** + * Creates an toast with text from resource and duration + * @param resId + * @param duration + */ + public static void makeToast(@StringRes int resId, int duration) { + makeToast(ConversationsPlusApplication.getAppContext().getString(resId), duration); + } + + private ConversationsPlusToast() { + // avoid instantiation - helper class + } + + /** + * Runnable to show the toast in an UI thread. + */ + static class ToastRunnable implements Runnable { + private boolean showErrorToast = false; + private CharSequence text; + private int duration; + + public ToastRunnable(CharSequence text, int duration, boolean isError) { + this.showErrorToast = isError; + this.text = text; + this.duration = duration; + } + + public ToastRunnable(CharSequence text, int duration) { + this.text = text; + this.duration = duration; + } + + @Override + public void run() { + Context applicationContext = ConversationsPlusApplication.getAppContext(); + LayoutInflater inflater = LayoutInflater.from(applicationContext); + View layout = inflater.inflate(R.layout.cplus_toast_container, null); + + TextViewUtil.setText(layout, R.id.cplus_toast_txt, text); + + if (this.showErrorToast) { + // Set image view to error icon + ImageView iv = (ImageView) layout.findViewById(R.id.cplus_toast_icon); + iv.setImageResource(R.drawable.ic_toast_error); + } + + Toast toast = new Toast(applicationContext); + toast.setDuration(duration); + toast.setView(layout); + toast.show(); + } + } +} diff --git a/src/main/res/drawable-hdpi/ic_toast.png b/src/main/res/drawable-hdpi/ic_toast.png new file mode 100644 index 00000000..fd5d1d8b Binary files /dev/null and b/src/main/res/drawable-hdpi/ic_toast.png differ diff --git a/src/main/res/drawable-hdpi/ic_toast_error.png b/src/main/res/drawable-hdpi/ic_toast_error.png new file mode 100644 index 00000000..31de0901 Binary files /dev/null and b/src/main/res/drawable-hdpi/ic_toast_error.png differ diff --git a/src/main/res/layout/cplus_toast_container.xml b/src/main/res/layout/cplus_toast_container.xml new file mode 100644 index 00000000..f2aa09e3 --- /dev/null +++ b/src/main/res/layout/cplus_toast_container.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file -- cgit v1.2.3