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:
- An empty button appears in the top left of the screen, presumably a snackbar action button
- The TextView doesn't disappear when the snackbar times out
- 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.