2

I have worked some time to get information how to show a custom Toast. This is my code

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE );
    View layout = inflater.inflate(R.layout.custom_toast, null);

    ImageView image = (ImageView) layout.findViewById(R.id.customtoastImageView1);
    image.setImageResource(R.drawable.stats);
    image.setColorFilter(More.getMore(getApplicationContext()).getColor(R.color.on));
    TextView text = (TextView) layout.findViewById(R.id.customtoastTextView1);
    text.setText("App opened " + getSharedPreferences(Prefs.PREFERENCE_APPS_COUNT, MODE_PRIVATE).getString(packagename, "0") + " times.");

    final Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.TOP, 0, 50);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

This code works in an Activity. But when I use it from my Service the App crashes. So my question is how to show a custom Toast from a Service. Thanks.

EDIT

A simple Toast works, the problem is the custom Toast.

larsaars
  • 2,065
  • 3
  • 21
  • 32
  • 2
    Possible duplicate of [How to display Toast from a Service? Sending message to a Handler on a dead thread](http://stackoverflow.com/questions/27358134/how-to-display-toast-from-a-service-sending-message-to-a-handler-on-a-dead-thre) – mrtpk Aug 31 '16 at 18:34
  • 1
    see [this](http://stackoverflow.com/a/27358216/6561141) It might help, – mrtpk Aug 31 '16 at 18:34
  • 1
    [Here](http://stackoverflow.com/questions/21515917/android-service-to-show-toast) you could see the solution ;) – Brais Aug 31 '16 at 18:38
  • Thanks I think that will help me – larsaars Aug 31 '16 at 18:39
  • I found my mistake now. Thanks some more research I spotted that the error was that it was in a TimerTask. – larsaars Sep 01 '16 at 07:53

0 Answers0