android - Progress Bar is not showing up when visibility is changed to visible in java code -
i'm making progress bar visible , invisible in async task. i'm doing db operations in async task while doing operations want show progressdialog. finishes operation. want make invisible. code below. problem progresssbar 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"> <!-- actionbar displayed @ 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 async task want make progress bar visible , 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 thread.sleep(millis) onpostexecute , put in doinbackground. onpostexecute, onpreexecute, runs on main thread sleep should put in background process.
your flow (at moment) this:
asynctaskcreatedpre-execute=progressbarvisibleonbackground= nothingpost-execute=thread sleep. --> stopping ui thread , view has no time update visibility because it's stoppedprogressbar=view.gone.
this not showing progress because thread being stopped immediatly , view changes applied once after return. use code below , works me.
another thing catch without try (?) code missing or not working. 2 fixes work. 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 relativelayout linearlayout , progressbar.. progressbar should visibility "gone" view space not kept used. can remove useless linearlayout , 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 hand, might not perfect)
Comments
Post a Comment