I'm making progress bar visible and invisible in async task. I'm doing some db operations in async task while doing operations I want to show progressdialog. as soon as it finishes its operation. I want to make it invisible. My code is below. Problem is progresssbar does not show up.
activity_xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_course_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.lms.CourseList">
<!-- The ActionBar displayed at the top -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/course_list"></ListView>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
I'm using an async task where I want to make progress bar visible and invisible
private class tmpCourseOfferingTask extends AsyncTask<String,Void,JSONObject> {
public tmpCourseOfferingTask(){
super();
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected JSONObject doInBackground(String...params){
return null;
}
@Override
protected void onPostExecute(JSONObject json) {
Thread.sleep(5000);
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.setVisibility(View.GONE);
}
First, remove the Thread.Sleep(millis) from onPostExecute and put it in the doInBackground. onPostExecute, as onPreExecute, runs on the main thread so the sleep should be put in the background process.
your flow (at the moment) is this:
asynctask
created pre-execute
= progressbar
visible onbackground
= nothing post-execute
= thread sleep
. --> this is stopping the UI thread and the view has no time to update the visibility because it's stopped progressbar
= View.GONE
. This is not showing the progress because the thread is being stopped immediatly and the View changes are applied once after a return. I use the code as below and it works for me.
Another thing is that your catch is without a try (?) so this code is missing something or is not working. With those two fixes it will work. do something like:
private class tmpCourseOfferingTask extends AsyncTask<String,Void,JSONObject> {
public tmpCourseOfferingTask(){
super();
}
@Override
protected void onPreExecute(){
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected JSONObject doInBackground(String...params){
try{
SystemClock.sleep(timeInMills);
}catch(Exception ignored){
}finally{
return null;
}
}
@Override
protected void onPostExecute(JSONObject json) {
progressBar.setVisibility(View.GONE);
}
}
Another fix:
You have a RelativeLayout with a LinearLayout and a progressbar.. also the progressbar should be with visibility "GONE" so that the view space is not kept used. You can remove the useless LinearLayout and do something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_course_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.lms.CourseList">
<include
amdrpod:id="@+id/includeId"
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_below="@+id/includeId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/course_list"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true" />
</RelativeLayout>
(wrote by hand, might be not perfect)
To make sure to see the ProgressBar use bringToFront()
at PreExecute() like this:
progressBar.bringToFront();
btw I think Pier's answer should do the trick.
Try to wrap your Progressbar inside linerlayout like below and then check show and hide functionality and use the liner layout ID to show and hide.
<LinearLayout
android:id="@+id/Progress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</LinearLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.