How can I set a ProgressBar in android using C#?

I am working on a Splash Screen. I want to put a progressbar that when it reaches to 100% it will proceed to the next layout. Can you give me codes using C# please?

Tagged:

Best Answer

Answers

  • johannes.jacobsjohannes.jacobs DEMember

    Where is your problem actually? Did you even begin with something? I dont think someone will give you your needs so that u can just copy and paste.

    This is your way:
    1. Create the XAML Activity Layout
    2. Create your C# Activity - Class
    3. Bind the ProgressBar from Layout to a variable by doing FindViewById in the OnCreate() function.
    4. Increase the ProgressBar with a Timer
    5. Check if ProgressBar's Value is 100 and start your next Activity with startActivity(typeof(youractivity)) ;

    More details in the guides. You should really check them out.

  • KevinSantosKevinSantos PHMember
    edited January 2016

    I started it by trying to understand the Java codes and converted it into C# codes by myself but it doesn't work. I'll try what you have said. Thanks.

  • KevinSantosKevinSantos PHMember

    Sorry I was working on android application. There is no timer here.

  • YkshLeoYkshLeo ✭✭✭✭ USMember ✭✭✭✭
    edited January 2016

    You can make use of System.Timers.Timer

        private System.Timers.Timer _timer;
        private int _countSeconds;
    
        void Main()
        {
            _timer = new System.Timers.Timer();
            //Trigger event every second
            _timer.Interval = 1000;
            _timer.Elapsed += OnTimedEvent;
            //count down 5 seconds
            _countSeconds = 5;
    
            _timer.Enabled = true;
        }
    
        private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            _countSeconds--;
    
            //Update visual representation here
            //Remember to do it on UI thread
    
            if (_countSeconds == 0)
            {
                _timer.Stop();
            }
        }
    

    Update your UI

    RunOnUiThread(() => {
        // update UI here
    });
    
  • KevinSantosKevinSantos PHMember

    I see, thank you very much @YkshLeo
    I will try this and I'll give you an update. I am new to C# so I need further knowledge about this :) Thank you for the second time ;)

  • KevinSantosKevinSantos PHMember
    edited January 2016

    @YkshLeo Can you please help me about this:
    If the progressbar is already done, I want to proceed to the next activity but there is an error.
    It says, "Code not running: The current thread is not currently running the code and the call stack is not obtained,"
    What is the meaning of this?

    public class MainActivity : Activity
    {
    private Timer _timer;
    private int _countSeconds;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            var progress = FindViewById<ProgressBar>(Resource.Id.progressBar1);
            progress.Max = 100;
            progress.Progress = 0;
            _timer = new System.Timers.Timer();
            //countdown to 100
            _countSeconds = 100;
            _timer.Enabled = true;
            //trigger event every second
            _timer.Interval = 1000;
            _timer.Elapsed += OnTimeEvent;
            Button btnStart = FindViewById<Button>(Resource.Id.button1);
            btnStart.Enabled = true;
                btnStart.Click += delegate
                {
                    StartActivity(typeof(sample));
                };
        }
    
        private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            var progress = FindViewById<ProgressBar>(Resource.Id.progressBar1);
            Button btnStart = FindViewById<Button>(Resource.Id.button1);
            _countSeconds-=10;
    
                RunOnUiThread(() =>
                {
                    progress.IncrementProgressBy(10);
                });
    
                if (_countSeconds == 0)
                {
                    _timer.Stop();
                    btnStart.Visibility = ViewStates.Visible;
                }
        }
    

    }

  • YkshLeoYkshLeo ✭✭✭✭ USMember ✭✭✭✭
    edited January 2016

    @KevinSantos,

    Try something like this :

    RunOnUiThread(() =>{
        prgsBar.IncrementProgressBy(10);
        CheckProgress(prgsBar.Progress);
    });
    

    public void CheckProgress (int progress)
    {
        lock (_lock)
        {
            if (progress >= 100)
            {
                Toast.MakeText (this, "Navigate to next", ToastLength.Long).Show ();
            StartActivity (typeof(SecoundActivity));        // Navigate to another activity
            }
        }
    }
    
  • KevinSantosKevinSantos PHMember

    @YkshLeo Still same error.
    It says: threadid=1: thread exiting with uncaught exception (group=0x416e2c08)
    An unhandled exception occured.

    public class MainActivity : Activity
    {
    private Timer _timer;
    private int _countSeconds;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            _timer = new System.Timers.Timer();
    
    
            //trigger event every second
            _timer.Interval = 1000;
            _timer.Elapsed += OnTimeEvent;
    
            //countdown to 100
            _countSeconds = 100;
    
            _timer.Enabled = true;
        }
    
        private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            var progress = FindViewById<ProgressBar>(Resource.Id.progressBar1);
            _countSeconds--;
    
                RunOnUiThread(() =>
                {
                    progress.IncrementProgressBy(10);
                    CheckProgress(progress.Progress);
                });
    
                if (_countSeconds == 0)
                {
                    _timer.Stop();
                }
        }
        private Object _lock;
        public void CheckProgress(int progress)
        {
            _lock = new System.Object();
    
            lock (_lock)
            {
                if (progress == 100)
                {
                    Toast.MakeText(this, "Navigate to next", ToastLength.Long).Show();
                    StartActivity(typeof(sample));
                }
            }
        }
    
        }
    
  • KevinSantosKevinSantos PHMember

    Thank you very much @YkshLeo for helping me. It is for my thesis, you really did a great help.
    Sorry if I disturb you. Till next time :)

  • JamshaidKamranJamshaidKamran ✭✭ PKMember ✭✭

    Can you share the xml part of your progressbar also? I am facing issues in rendering of progressbar. it is not being displayed on the activity

  • kingkanekingkane ✭✭ Member ✭✭

    This is what I did:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Loading..."
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
    
        <ProgressBar
            android:layout_width="fill_parent"
            android:layout_height="10.0dp"
            android:id="@+id/progressBar1"
            android:indeterminate="true"
            android:visibility="visible"
            android:indeterminateTint="@android:color/holo_orange_light"
            android:indeterminateTintMode="multiply"
            style="@android:style/Widget.ProgressBar.Horizontal" />
    
    </LinearLayout>
    
  • JunedJuned Member
    edited October 10

    YkshLeo, Thanks. It worked. The only resource available on the internet that shows working with actual ProgressBar in Xamarin.Android, all others just show ProgressDialog (deprecated) in the name of ProgressBar.

Sign In or Register to comment.