Search code examples
c#xmlandroid-layoutxamarin.androidandroid-view

Hide View after View finishes its job Xamarin


I am trying this new Konfetti View that shows Confetti on the screen, found here: https://github.com/DanielMartinus/Konfetti

I want this view to disappear after the Confetti stops falling, however with the code I have currently, the Konfetti View disappears before the Confetti even starts falling.

ConfettiActivity:

using System;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Text;
using Android.Views;
using Android.Widget;
using NL.DionSegijn.Konfetti;
using NL.DionSegijn.Konfetti.Models;

namespace ConfettiTest
{
    [Activity(Label = "ConfettiActivity")]
    public class ConfettiActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.confetti_activity);
            KonfettiView konfettiView = (KonfettiView)FindViewById(Resource.Id.viewKonfetti);
            int num = 0;
            while (num != 2)
            {
                if (num == 0)
                {
                    konfettiView
                    .Build()
                    .AddColors(Color.Yellow, Color.Green, Color.Magenta)
                    .SetDirection(0.0, 359.0)
                    .SetSpeed(1f, 5f)
                    .SetFadeOutEnabled(true)
                    .SetTimeToLive(4000L)
                    .AddSizes(new Size(12, 5f))
                    .StreamFor(400, 4000L);
                }
                else if (num == 1)
                {
                    konfettiView.Visibility = ViewStates.Gone;
                }
                else
                {
                }
                num++;
            }
        }
    }
}

confetti_activity.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <nl.dionsegijn.konfetti.KonfettiView
        android:id="@+id/viewKonfetti"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="Confetti is finished"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/confettifinished"
        android:textSize="50px"/>
</LinearLayout>

This answer :https://stackoverflow.com/a/6690607/13370965 won't work, cause in my proper code, I have more than 1 textview.

Another alternative is to put the konfettiView in the background, so you can see the textViews and the confetti at the same time, but I can't figure out how to this either.


Solution

  • You can use a timer to only show the view for a certain amount of time.

    Could look something like this:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.confetti_activity);
    
        KonfettiView konfettiView = (KonfettiView)FindViewById(Resource.Id.viewKonfetti);
    
        konfettiView
        .Build()
        .AddColors(Color.Yellow, Color.Green, Color.Magenta)
        .SetDirection(0.0, 359.0)
        .SetSpeed(1f, 5f)
        .SetFadeOutEnabled(true)
        .SetTimeToLive(4000L)
        .AddSizes(new Size(12, 5f))
        .StreamFor(400, 4000L);
    
        //Create and run a timer
        Device.StartTimer (new TimeSpan (0, 0, 5000), () =&gt; //(5000 = 5sec)
        {
           Device.BeginInvokeOnMainThread (() =&gt; 
           {
              //Hide the konfetti
              konfettiView.Visibility = ViewStates.Gone;
           });
           return false;
        });          
    }