So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).
To work towards this goal, I found this post on how to only show a toast if none are being shown.
I have modified the code to fit both requirements.
private Toast toast;
public void showAToast (String st, boolean isLong){
try{
toast.getView().isShown();
String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
if(!text.equalsIgnoreCase(st)){
//New message, show it after
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
} catch (Exception e) {
//New message
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
}
My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.
Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.
The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.
So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.
I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
In the OnCreate:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
//And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
// If you need to just cancel an existing toast (before it times out) use:
toast0.cancel();
toast1.cancel();
Quotation
You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.
Toast mToast;
public void showToast(CharSequence message, int during){
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), message, during);
} else {
mToast.setText(message);
}
mToast.show();
}
--↓---↓----↓---update--↓----↓---↓--↓
Sorry about that I miss your point above.
I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.
But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.
Related
I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless
I'm making an app that can take photos. If the taken photo is saved into a folder I want a toaster message to appear.
However with my current code when I click the camera button, the toaster just comes up without waiting for the photo to be taken.
Below is the source code to take the photo:
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
public void onClickbtnCamera(View v){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Uri uriSavedImage=Uri.fromFile(
new File("/storage/emulated/0/DCIM/Camera","QR_"+timeStamp+ ".png"));
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, 1);
}
The toaster I want to show:
Toast toast= Toast.makeText(getApplicationContext(),
"Picture was taking", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
Can you tell me why my code is failing to do the intended job?
#Craig The question you've asked isn't clear/complete.
Assuming from the information you have provided, that you want a message to be displayed on a screen once a picture is saved. And you are using
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
Have an event listener on imageIntent object. Register that event to the method that contains this piece of code.
Toast toast= Toast.makeText(getApplicationContext(),
"Picture was taking", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
PS: This might not be the actual solution but hope should help you to give a direction
The toast that says not reachable is not showing up, but all the above are showing up, why does my code stop execution on that line?
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}
I copied this code into my test project and added a toast to the if and the else if for gpsEnabled and netOn.
It looks like the first if is the one that is getting executed as gpsEnabled is true.
Hope this helps, good luck.
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
Imloca returns null.Problem is this.
Here is my code, I have a few questions as I am completely new to this whole service receiver thing.
It works fine. I get to checkout,I purchase, google says I will receive my item soon.
My question is that after the application gets closed, or after the activity that contains the handler is closed, will my app still receive the notification, and the gems be updated?
Any pointers or clues to better help me understand this would be awesome =D
I checked and my service is still running in the background, so this is good :)
But it is taking forever to update, or to receive
does anyone know?
Button bPurchase = (Button) findViewById(R.id.bPurchase);
bPurchase.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox confirm = (CheckBox) findViewById(R.id.bConfirm);
if (!confirm.isChecked()) {
Toast msg = Toast.makeText(PurchaseActivity.this, "Please Check Confirmation box", Toast.LENGTH_SHORT);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
return;
}
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i("","Can't purchase on this device");
}
}
});
}
public Handler mTransactionHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.i("", "Transaction complete");
Log.i("", "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
Log.i("", "Item attempted purchase is: "+BillingHelper.latestPurchase.productId);
if(BillingHelper.latestPurchase.isPurchased()){
long g = GemUpgrades.getGems();
g = g + 1000;
GemUpgrades.setGems(g);
Toast msg1 = Toast.makeText(PurchaseActivity.this, "Received", Toast.LENGTH_SHORT);
msg1.setGravity(Gravity.CENTER, msg1.getXOffset() / 2, msg1.getYOffset() / 2);
msg1.show();
} else {
// Failure
}
};
};
SO I discovered that i was not receiving my calls because I forgot to set the Handler as the Handler. Also It seemed it updated outside the activity, and pretty quick too. But would it still update if the app has been closed before it gets its updat
I have an activity which gets called by 2 intents, one after a simple menu-selection and the other way by a intent after a deletion of an item in a database. However, I wanted to display in the called activity a little Toast, but only when it's opened through the intent of the deletion. I thought of following solution
public void intentCheck(){
Log.d("ShowActivity","intentCheck() called");
Bundle extras = getIntent().getExtras();
if (extras != null){
String check = extras.getString("AdvancedViewActivityCall");
if(check == "calling"){
Log.d("ShowActivity","delete-intent succeeded");
Toast success = new Toast(ShowActivity.this);
success.makeText(ShowActivity.this, "Deletion succeded", Toast.LENGTH_LONG);
}
}
but it doesn't work... somehow, no toast gets displayed.
edit:// i applied success.show(); now, but now i get a RunetimeException O.o ( http://pastebin.com/Th3NY5d0 )
edit: SOLUTION: Toast.makeText(context, text, duration).show(); //seems to be the "static way", which eclipse proposed
Have you tried if ("calling".equals(check)) instead of if(check == "calling") ?
EDIT:
try Toast.makeText(context, text, duration).show();
you have to call show method for toast until otherwise toast will not display.
success.show();