0

I'm trying to use a Custom Toast with ImageView and TextView. I want my Toast to disappear when I touch anywhere (clicking button, touching layout...), but it doesn't.

I read the Toast.class file and tried using the cancel() method before a new Toast was called, but that didn't solve anything. Can anyone give me a solution?

My CustomToast.java:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View v = new View(context);
v = inflater.inflate(R.layout.custom_toast, (ViewGroup) v.findViewById(
        R.id.layout_custom_toast));

layout = (RelativeLayout) v.findViewById(R.id.layout_custom_toast);
tvToast = (TextView) v.findViewById(R.id.tv_custom_toast);
tvToast.setText(text);

ivToast = (ImageView) v.findViewById(R.id.iv_custom_toast);
layout.setBackgroundResource(R.drawable.border_style_red);
ivToast.setBackgroundResource(R.drawable.warning);

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
Taeng
  • 21
  • 1
  • Have you seen this question? http://stackoverflow.com/q/10070108/1979347 – Rohan Kandwal May 11 '15 at 07:57
  • Please check this http://blog.ravinishad.com/post/84906000396/toasts-dont-annoy-me – Napolean May 11 '15 at 07:58
  • Solution is to build a `Toast` based on an (dismissable) `AlertDialog`. Lots of tutorials and questions about it. – shkschneider May 11 '15 at 08:05
  • Thanks for responses. @shkschneider - I solved this problem by initializing toast object with Toast.makeText(...), removing all views and adding ImageView and TextView. – Taeng May 11 '15 at 09:05
  • Glad you solved it. Post your solution as an answer, then accept it yourself to close this question please. – shkschneider May 11 '15 at 10:01

1 Answers1

0

Solution : I used Toast.makeText(...) instead of new Toast(..) because it inflates resource in hidden api(com.android.internal.R) and you cannot access to it. Initializing toast object with Toast.makeText(...) makes you access to hidden api simply.

Toast toast = Toast.makeText(context, text, duration);

layout = (LinearLayout) toast.getView();
layout.removeAllViews();
layout.setOrientation(LinearLayout.HORIZONTAL);

int dftPadding = (int) StaticValues.dpToPixel(context, 10);
layout.setPadding(dftPadding, dftPadding, dftPadding, dftPadding);
ivToast = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, (int) StaticValues.dpToPixel(context, 5), 0);
ivToast.setLayoutParams(params);
layout.setBackgroundResource(R.drawable.border_style_red);
ivToast.setBackgroundResource(R.drawable.warning);
layout.addView(ivToast);
tvToast = new TextView(context);
tvToast.setTextColor(Color.BLACK);
tvToast.setTypeface(null, Typeface.BOLD);
tvToast.setText(text);
layout.addView(tvToast);
toast.show();
Taeng
  • 21
  • 1