Skip to content

Commit

Permalink
Toast: Add methods for posting toasts to the UI thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schildbach committed Apr 13, 2018
1 parent 31169c1 commit 6f9676c
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion wallet/src/de/schildbach/wallet/util/Toast.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,29 +18,68 @@
package de.schildbach.wallet.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

/**
* @author Andreas Schildbach
*/
public class Toast {
private final Context context;
private final Handler handler = new Handler(Looper.getMainLooper());

public Toast(final Context context) {
this.context = context;
}

public final void postToast(final int textResId, final Object... formatArgs) {
handler.post(new Runnable() {
@Override
public void run() {
toast(textResId, formatArgs);
}
});
}

public final void toast(final int textResId, final Object... formatArgs) {
customToast(textResId, android.widget.Toast.LENGTH_SHORT, formatArgs);
}

public final void postToast(final CharSequence text) {
handler.post(new Runnable() {
@Override
public void run() {
toast(text);
}
});
}

public final void toast(final CharSequence text) {
customToast(text, android.widget.Toast.LENGTH_SHORT);
}

public final void postLongToast(final int textResId, final Object... formatArgs) {
handler.post(new Runnable() {
@Override
public void run() {
longToast(textResId, formatArgs);
}
});
}

public final void longToast(final int textResId, final Object... formatArgs) {
customToast(textResId, android.widget.Toast.LENGTH_LONG, formatArgs);
}

public final void postLongToast(final CharSequence text) {
handler.post(new Runnable() {
@Override
public void run() {
longToast(text);
}
});
}

public final void longToast(final CharSequence text) {
customToast(text, android.widget.Toast.LENGTH_LONG);
}
Expand Down

0 comments on commit 6f9676c

Please sign in to comment.