3

I am getting various unexpected behaviors when trying to implement a snackbar with a custom view in Xamarin forms.

This is the function I am calling (from a dependency service):

    public void MySnackBar(string argument, string title, string message, string action_text, string action, MySnackBarPosition position)
    {
                    Activity activity = Xamarin.Essentials.Platform.CurrentActivity; //.Current.Activity;
        Android.Views.View view = activity.FindViewById(Android.Resource.Id.Content);
        var snack_view = activity.LayoutInflater.Inflate(Droid.Resource.Layout.my_snackbar, (ViewGroup)view);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WrapContent,
            FrameLayout.LayoutParams.WrapContent
        );

        snack_view.LayoutParameters = lp;

        var snack_label = snack_view.FindViewById<TextView>(Droid.Resource.Id.my_snack_text);
        snack_label.Text = "You're doing something wrong";
        snack_label.Tag = $"{argument}:{action}";
        snack_label.Click += OnSnackClick;

        var snack = Snackbar.Make(activity, snack_view, "", Snackbar.LengthLong);            
        snack.Show();

    }

    private void OnSnackClick(object sender, EventArgs args)
    {
        var v = (View)sender;

        var tag = v.Tag;
        var data = tag.ToString().Split(':');
        //var argument = data[0];
        //var action = data[1];

        MyApp.App.Instance.HandleSnackbarAction("","");

    }

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="250dp"
android:layout_gravity="end"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="20dp"
android:background="@android:color/transparent">

<TextView
    android:id = "@+id/my_snack_text"
    android:elevation="8px"
    android:background="#fff"
    android:layout_marginEnd="20dip"
    android:layout_marginTop="20dip"
    android:padding = "20dip"
    android:gravity="center"
    android:text="hello hsdkfhdskjh dsfhsdjkh fjsdkfh sdjkhf dskj hfjkdshfjkdshfjkdsh"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>

</LinearLayout>

When I call this code for a first time the TextView displays with the correct text, but there are two big issues:

  1. An empty button appears in the top left of the screen, presumably a snackbar action button
  2. The TextView doesn't disappear when the snackbar times out
  3. The v.Tag inside the OnSnackClick is null

If I call the code a second time, the same issues persist but on this occasion the TextView's text isn't even set and only the default text from the xml file is displayed.

First Call: First call

Subsequent Calls: Subsequent calls

LairdPleng
  • 948
  • 3
  • 9
  • 28
  • Verify that you are calling on UI thread? I was thinking that might explain text not updating second time. I don't have any ideas about the other symptoms. – ToolmakerSteve Jan 21 '23 at 18:20
  • @ToolmakerSteve Thanks for the idea. The code is running on the UI thread in this particular case, though there may be cases where it would have been in another thread, so I have wrapped the code in a "runOnUiThread call, but the problem still persists :( – LairdPleng Jan 23 '23 at 03:22
  • Could you provide some more relevant code so that I can reproduce the situation you described? – Jianwei Sun - MSFT Jan 24 '23 at 09:28
  • @JianweiSun-MSFT This is the only code relevant to the situation – LairdPleng Jan 25 '23 at 10:02
  • @LairdPleng I tested the code you provided but failed to run... About custom snackbar, you can refer to this case: [how to customize snackBar's layout?](https://stackoverflow.com/questions/32453946/how-to-customize-snackbars-layout) and [How to create android snackbar with custom layout](https://stackoverflow.com/questions/33885424/how-to-create-android-snackbar-with-custom-layout?noredirect=1&lq=1). Wish it can help you. – Jianwei Sun - MSFT Feb 01 '23 at 09:51

0 Answers0