Android - ProgressBar setVisibility for GONE not working

I added a ProgressBar to fragments in my application. I set it for the two main snippets (used as tabs) as follows:

ProgressBar in activity_main.xml :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"> <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_centerVertical="true" /> </RelativeLayout> 

Setting ProgressBar VISIBLE and GONE :

 spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1); spinner.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); 

This works without a problem. I tried adding another ProgressBar to another snippet that has a WebView :

ProgressBar in fragment_article.xml :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="info.androidhive.slidingmenu.ArticleFragment$PlaceholderFragment" > <WebView android:id="@+id/webPage" android:layout_height="wrap_content" android:layout_width="wrap_content"/> <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_centerVertical="true" /> </RelativeLayout> 

Visibility setting:

 spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1); spinner.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); 

The visibility setting is the same as the previous code, but for some reason this does not set the ProgressBar to GONE . Not sure what happened.

I tried using clearAnimation , as suggested here by Android, setVisbility does not work in RelativeLayout , but still nothing.

 spinner.clearAnimation(); spinner.setVisibility(View.GONE); 
+8
android progress-bar android-fragments android-spinner
source share
5 answers

Check this code:

 spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1); 

If you use fragments, it should be something like this:

 spinner = (ProgressBar)viewIinflated.findViewById(R.id.progressBar1);//same case with dialogs 

If you use activity, then:

 spinner = (ProgressBar)findViewById(R.id.progressBar1); 
+6
source share

It is likely due to the fact that View.GONE will prevent the View from drawing on the screen, while another component of the android:layout_align[POSITION] View can be defined as View , so their position cannot be calculated.

View.INVISIBLE will work because it just makes it invisible, but the View still exists, and other View can calculate its position if it is set to

+1
source share

I had the same problem (progressBar.setVisibility () did not work).

As stated in @Lllegal Argument,

 // in Activity ProgressBar mProgressBar = (ProgressBar)findViewById(R.id.progressBar1); mProgressBar.setVisibility(View.GONE); 

should work if this code runs on uiThread (mainThread).

My problem was that I tried to run the code not on uiThread. So I solved the problem by changing the code from

 mProgressBar.setVisibility(View.GONE); 

to

 runOnUiThread(new Runnable() { @Override public void run() { mProgressBar.setVisibility(View.GONE); } }); 
+1
source share

This work for me:

 rootView.findViewById(R.id.progress_bar).setVisibility(View.GONE); 
-one
source share

progressBar.setVisibility (ProgressBar.VISIBLE);

-one
source share

All Articles