Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ProgressBar setVisibility to GONE not working

I've been adding a ProgressBar to the fragments in my app. I've set it up to the two main fragments (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 any problems. I've tried to add another ProgressBar to another fragment which 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>

Setting Visibility:

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

spinner.setVisibility(View.GONE);

Setting the visibility the same way as the previous code but for some reason this is not setting the ProgressBar to GONE. Not sure what's wrong.

I've tried using clearAnimation as suggested here Android, setVisbility to gone not working in RelativeLayout but still nothing.

spinner.clearAnimation();
spinner.setVisibility(View.GONE);
like image 616
j.grima Avatar asked Jun 21 '14 15:06

j.grima


2 Answers

I had a same issue (progressBar.setVisibility() was not working).

As @Illegal Argument said,

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

should be working, if that code runs on uiThread(mainThread).

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

mProgressBar.setVisibility(View.GONE);

to

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        mProgressBar.setVisibility(View.GONE);
    }
});
like image 126
KevinRyu Avatar answered Sep 18 '22 16:09

KevinRyu


Check this code:

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

If you are using fragments it should be like this:

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

If you are using activity then:

spinner = (ProgressBar)findViewById(R.id.progressBar1);
like image 39
Illegal Argument Avatar answered Sep 17 '22 16:09

Illegal Argument