user8467834
user8467834

Reputation: 25

Cancel toast message on touch

I'm trying to make my toast message disappear prematurely when the background is touched. The toast should disappear when every part of the screen is touched... I know the method is .cancel but I can't utilize it properly.. this is what I tried:

I tried with the .setontouchlistener method but it doesn't work... I'm new in android development so it would be appreciated to have a sample code to learn from... I'll show you my code, this is it:

public class MainActivity extends AppCompatActivity {
private LayoutInflater layoutInflater;
private RelativeLayout relalayout
private LinearLayout linelayout;
private ScrollView scrollayout;
public Toast toast;
private ImageView backgroundimg;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activitymain);
    Bundle extradata1 = getIntent().getExtras();
    String textString = extradata1.getString("ImportedData");
    TextView mytext = (TextView) findViewById(R.id.text);
    relalayout = (RelativeLayout) findViewById(R.id.relalayout);
    linelayout = (LinearLayout) findViewById(R.id.linelayout);
    scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
    backgroundimg = (ImageView) findViewById(R.id.imageView2);
    toast = new Toast(this);

 if (textString.equals("firstimported")) {

        String mytxt = "";
        StringBuffer sbuffer = new StringBuffer();
        InputStream is = this.getResources().openRawResource(R.raw.firsttext);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));


        try {

            while ((mytxt = reader.readLine()) != null) {
                sbuffer.append(mytxt + "\n");

            }

            mytext.setText(sbuffer);
            is.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
        String text = sbuffer.toString();
        SpannableString ss = new SpannableString(text);

ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
    Toast toast = Toast.makeText(MainActivity.this, "this is the toast",
    Toast.LENGTH_SHORT);
    toast.show();}
    @Override
    public void updateDrawState(TextPaint ds) {

        super.updateDrawState(ds);
        ds.setColor(Color.WHITE);
        ds.setUnderlineText(false);}
    };

    scrollayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        toast.cancel();
        return false;
    }
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

mytext.setText(ss);
        mytext.setMovementMethod(LinkMovementMethod.getInstance());

}
}

public void onBackPressed() {
    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
    startActivity(intent);
    finish();
}

the activity crashes when I touch the screen.. so it just won't work.

Upvotes: 1

Views: 1091

Answers (3)

Brijesh Joshi
Brijesh Joshi

Reputation: 1857

Try this

Toast toast; 
@Override
public void onClick(View widget) {
    toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
    toast.show();
}
scrollayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(toast != null)
            toast.cancel();
        return super.onTouch(v, event);
    }
});

Update Answer as per the code

public class MainActivity extends AppCompatActivity {
    private ScrollView scrollayout;
    public Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitymain);
        TextView mytext = (TextView) findViewById(R.id.text);
        scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
        StringBuffer sbuffer = new StringBuffer();
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        sbuffer.append("1234567890\n");
        String text = sbuffer.toString();
        SpannableString ss = new SpannableString(text);
        ClickableSpan clickspan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
                toast.show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.WHITE);
                ds.setUnderlineText(false);
            }
        };
        scrollayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (toast != null) {
                    toast.cancel();
                    toast = null;
                }
                return false;
            }
        });
        ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mytext.setText(ss);
        mytext.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

I have changed some values that you used as I didn't have all the resources, But major change you need to do is in the ClickSpan initialization and scrolllayout touch event. Please compare them. I got the following output with scrolling after the toast has been shown. enter image description here

Applogies for the long size gif.

Upvotes: 1

Kartika Vij
Kartika Vij

Reputation: 211

Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.

Upvotes: 0

Tiago Ornelas
Tiago Ornelas

Reputation: 1119

You have to declare the Toast object on your activity's variable

private Toast toast;

And then initialize it on your onClick method:

toast = Toast.makeText(MainActivity.this, "this is the toast",Toast.LENGTH_SHORT);
toast.show();

Also, on your onTouch you should check if the toast object is nul:

if(toast != null){
   toast.cancel();
}

Upvotes: 2

Related Questions