Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Show Progress Bar While Changing Fragments

Hi everyone!
In my application I have multiple fragments that each of their data depends on the item user clicks in the previous fragment. Then I replace the old fragment with the new one. This takes some time because I'm fetching some data with rest request (depending on the data in the previous fragment) and the first fragment freezes until data in the second fragment is ready to be shown.
This could be very frustrating for the user, so I want to show a spinning progress bar until the second fragment is ready to be shown.
This is my code:

//On The Item Click In The Previous Fragment
public void InsideAdUserProfile_Click(object sender, int position)
            Bundle insideArgs = new Bundle();
            insideArgs.PutString("cityname", usercity.cityname);
            insideArgs.PutString("ud", user.usertoken);
            insideArgs.PutString("sesid", methods.CheckSession());
            insideArgs.PutString("token", ads[position].token);
            lastAdToken = ads[position].token;
            insideAdFrag.Arguments = insideArgs;
            ReplaceFragment(insideAdFrag, "insideAdFrag");

//My Replace Fragment Method
public void ReplaceFragment(Android.App.Fragment fragment, string tag)
        if (fragment.IsVisible)
        var trans = FragmentManager.BeginTransaction();
        trans.Replace(Resource.Id.fragmentContainer, fragment, tag);
        currentFragment = fragment;

//Sending Requests
public Task<string> RestRequest(string url, string type, string parameters)
        var content = new TaskCompletionSource<string>();
        var restClient = new RestClient(domainName);
        RestRequest request;
        if (type == "post")
            request = new RestRequest(url, Method.POST);
            request = new RestRequest(url, Method.GET);
        if (parameters != "")
            string[] couple = parameters.Split('\n');
            foreach (string coup in couple)
                string[] temp = coup.Split(':');
                request.AddParameter(temp[0], temp[1]);
        restClient.ExecuteAsync(request, response =>
        return content.Task;

//Inside The Second Fragment OnCreateView method
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    View view = inflater.Inflate(Resource.Layout.insidead, null);
    string insideadContent = methods.RestRequest(URLWithDataInTheArguments, "post", "").Result;
    dynamic insidead = Newtonsoft.Json.JsonConvert.DeserializeObject(insideadContent);
    //The Rest of My Code Irrelevant To The Subject

Best Answer


  • SaharPournassehSaharPournasseh USMember ✭✭
    edited August 2016

    Thanks @LuisRodriguez92 I got it working.
    And also I changed it a little bit to look more like a spinner.
    This is the style I used if anyone wanted to do the same:

    In theme.xml:

    <style name="SpinnerTheme" parent="android:Theme.Holo.Dialog">
        <item name="android:alertDialogStyle">@style/CustomAlertDialogStyle</item>
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">12sp</item>
      <style name="CustomAlertDialogStyle">
        <item name="android:bottomBright">@color/transparent</item>
        <item name="android:bottomDark">@color/transparent</item>
        <item name="android:bottomMedium">@color/transparent</item>
        <item name="android:centerBright">@color/transparent</item>
        <item name="android:centerDark">@color/transparent</item>
        <item name="android:centerMedium">@color/transparent</item>
        <item name="android:fullBright">@color/transparent</item>
        <item name="android:fullDark">@color/transparent</item>
        <item name="android:topBright">@color/transparent</item>
        <item name="android:topDark">@color/transparent</item>

    in colors.xml

    <color name="transparent">#00000000</color>

    in my initialize code:

    ProgressDialog progress = new ProgressDialog(this.Activity, Resource.Style.SpinnerTheme);
  • veeru3112veeru3112 INMember ✭✭

    I am also facing same problem, i have 3 tabs in a activity and for tabs i am using Fragment, whenever i am loading data i want to show the Progress Dialog but i am unable to see the dialog ???????? can you help me for this ??

Sign In or Register to comment.