-1

In some of device like Samsung Galaxy Grand Duos android 7.1.2 my custom Toast giving this Exeption

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
          at ir.mrchabok.chabokpassenger.Helpers.Helper.MyToast(Helper.java:196)
          at ir.mrchabok.chabokpassenger.Activity.MapsActivity$4.onClick(MapsActivity.java:345)
          at android.view.View.performClick(View.java:5637)
          at android.view.View$PerformClick.run(View.java:22433)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:153)
          at android.app.ActivityThread.main(ActivityThread.java:6244)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)

And my custom Toast code is :

public static void MyToast(Context context, String text){
    Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    TextView tvToast = (TextView) toastLayout.getChildAt(0);
    tvToast.setTypeface(globTypeFace);
    toast.show();
}

As you know toast layout is LinearLayout and I didn't change the layout.

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Radesh
  • 13,084
  • 4
  • 51
  • 64
  • 1
    Evidently Samsung use a different implementation for `Toast`s. In truth, your solution is a bit hacky and relies on assumptions - I would suggest you take an alternative approach, like setting the font type using a `SpannableString` (see [this](https://stackoverflow.com/a/23637075/1219389) answer) or, if customisation is important, using a custom view for your toasts with `Toast#setView` – PPartisan Sep 15 '18 at 12:56
  • 1
    @PPartisan tanks , that's seems correct – Radesh Sep 15 '18 at 13:07
  • @Radesh its working fine in my application bro – Ashvin solanki Sep 15 '18 at 13:21

1 Answers1

0

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

It seems that the view is a type of RelativeLayout not LinearLayout. So you can try to change the LinearLayout to RelativeLayout.

BTW, You'd better to use Toast#setView

toBe NULL
  • 26
  • 3