9

I want to change all toasts in my App by editing themes.xml.

I am using <item name="buttonStyle">@style/MyButton</item> to change my buttons, is there something similar with Toasts, or do I have to create and use MyToast class which extends the built-in Toast?

Gary
  • 13,303
  • 18
  • 49
  • 71
guness
  • 6,336
  • 7
  • 59
  • 88

2 Answers2

6

You can change the background of your Toast with this:

<style name="myTheme" parent="@android:styles/Theme.Holo">
  <item name="android:toastFrameBackground">@android:drawable/my_toast</item>
</style>
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • thank you Ahmad, is there any other customization rather than background? secondly is there a table for all item names such as `android:toastFrameBackground` – guness Apr 07 '13 at 10:07
  • 1
    @bluebrain you can't set the gravity etc. with styles (as fas as I can tell), you'll have to do that programmatically. Android is open source, so you can find everything in here: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml – Ahmad Apr 07 '13 at 16:09
  • 3
    Can you change the background? As another SO posts point out, toastFrameBackgound isn't public. On API 18, I get an error saying no resource was found matching that name. http://stackoverflow.com/questions/19215326/styles-toast-background-no-resource-found-that-matches-the-given-name – spaaarky21 Mar 03 '14 at 22:48
  • 1
    I cannot find `android:toastFrameBackground` in SDK 27. – AutonomousApps Mar 15 '18 at 22:47
  • This doesn't work for all devices, since "toastFrameBackground" isn't part of the public api {https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/res/values/public.xml}. The safe solution that works on all devices is just to use the `Toast.setView(view)`. – Henry Feb 17 '21 at 05:06
-3

Try this:

 Toast toast=new Toast(this);
     LayoutInflater inflater=this.getLayoutInflater();
     View toastView=inflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toastView));
     TextView txtDate=(TextView)toastView.findViewById(R.id.txtDate);
     txtDate.setText("toast appeared at "+Calendar.getInstance().getTime().toLocaleString());
     toast.setGravity(Gravity.CENTER, 0, 0);
     toast.setView(toastView);
     toast.show();
R KiranKumar
  • 825
  • 1
  • 8
  • 27
  • 8
    I don't see anything related to 'theme'(which is related to global, not a single toast) here, can you please elaborate the answer? – guness Apr 05 '13 at 12:47