Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar Spinner for Async Task doesn't work

My problem is that when i click button "Click" to call my API it should show my progressBar(spinner) while I'm calling API. Instead my application freeze for less than a second and then when it's done calling it shows my loading spinner for a brief time (it just flash)

Here is my code

private ProgressBar spinner;
public View onCreateView(...)
{
    spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
    spinner.setVisibility(View.GONE);
    ...
}



private class MyTask extends AsyncTask<String, Void, Void> {

    String pageContent = "";
    DataOutputStream wr;


    @Override
    protected void onPreExecute() {
        spinner.setVisibility(View.VISIBLE);
    }

    @Override
    public Void doInBackground(String... params) {

        requestResult.setSuccess(false);
        HttpURLConnection connection;

        try {
            String url = "myURL";

            //I only call my Api here. I delete rest so this won't bother you

            requestResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        JsonParser parser = new JsonParser();
        //Same here i delete nonimportant 

        MyResponse = new Gson().fromJson(jsonContent, MyResponse.class);
        done();
        spinner.setVisibility(View.GONE);
    }

}

I've tried hounder different things it always freeze for a second and then my loading spinner flash and my content from API is shown.

Can you help me with that one, please?

My xml file

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view_send"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/buttonSend"
            android:visibility="gone"/>
    </RelativeLayout>
</ScrollView>
like image 448
Rok Avatar asked Jul 14 '26 03:07

Rok


1 Answers

Try the below snippet. It does not require spinner to be declared in layout file.

AsyncTask<String, String, String> asyncObject =
new AsyncTask<String, String, String>() {
    ProgressDialog progDailog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progDailog =
                new ProgressDialog(Activity.this);
        progDailog.setMessage("Loading");
        progDailog.setCancelable(false);
        progDailog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        //Do background stuff here
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        progDailog.cancel();
        //Do post background stuff here.
    }
};
asyncObject.execute(null, null, null);
like image 96
Balu SKT Avatar answered Jul 21 '26 13:07

Balu SKT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!