pratick
pratick

Reputation: 323

Prevent toast Message from getting cancelled on touch

I am developing a game in which I am displaying toast messages for instructions.

The problem is as soon as the user touches the screen, toast message is getting cancelled.

How can I prevent onTouch event from cancelling toast message?

Code used to create toast message:

    Toast.makeText(context, toastMsg, toastLength);

Thanks for your help!!!

Upvotes: 4

Views: 740

Answers (3)

Androidcoder
Androidcoder

Reputation: 4679

I'm a game developer, so this is a big issue for me. Toasts not only disappear on screen taps, but are also vulnerable to screen drags. OpenGL offers no text support, so without toasts I ended up having to create and display a bitmap of any temporary text I wanted to show. This was hugely time and memory consuming, particularly for multiple language support.

My hack is to use a global variable for the toast string, set a Long variable to the System time it intitially launched, and then reshow the toast in my downtouch interface code if any downtouch happens before 3500 milliseconds (duration Toast.LENGTH_LONG) has passed.

for (int downtouch = 0; downtouch < downTouches.length; downtouch++) {
  ...
   if(downtouch==0&&System.currentTimeMillis()<toastmessagestart+3500) {//the first of any simultaneous touches
            ((Activity) getContext()).runOnUiThread(new Runnable() {
                Toast toast;
                public void run() {
                    if(System.currentTimeMillis()<toastmessagestart+1500){//duration Toast.LENGTH_LONG - duration Toast.LENGTH_SHORT
                        toast = Toast.makeText(getContext(), globaltoaststring, Toast.LENGTH_LONG);
                    }else{
                        toast = Toast.makeText(getContext(), globaltoaststring, Toast.LENGTH_SHORT);
                    }
                    toast.show();
                }
            });
   }

This hack isn't perfect, as the perceived length of the toast will vary a bit depending on screen interaction, but it will be at least 3500 milliseconds. Also there may be intermittent fading, but surprisingly not much.

Upvotes: 0

Kosh
Kosh

Reputation: 6334

here is a transparent dialog with a timer that i often use in my helper class to give guide or instructions.

public static void Toaster(final Context ctx, final String text) {
    final Dialog dialog = new Dialog(ctx);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(
            android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,
            android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.guide);
    TextView guide = (TextView) dialog.findViewById(R.id.g_text);
    guide.setText(text);
    Button buy = (Button) dialog.findViewById(R.id.gotit);
    buy.setVisibility(View.INVISIBLE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    dialog.show();
    dialog.getWindow().setAttributes(lp);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, 2000);
}

and here is the xml file.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|center"
        android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center"
        android:textColor="#ffffff"        
        android:id="@+id/g_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >
    <Button
        android:id="@+id/gotit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textColor="#2f72da"
        android:layout_gravity="bottom"
        android:text="@string/got_it" />
    </LinearLayout>
</LinearLayout>

and simply call it by using

Toaster(YourClassName.this,"Your Text");

Upvotes: 1

Alp
Alp

Reputation: 1893

you can use alert buttons

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Warning");
            builder.setMessage("this is a question?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     //do sth

                    dialog.dismiss();
                }

            });

            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do sth else
                    dialog.dismiss();
                }
            });

            alert = builder.create();
            alert.show();

Upvotes: 0

Related Questions