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.