Android - ProgressBar setVisibility to GONE not working
Asked Answered
C

5

13

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);
Concordia answered 21/6, 2014 at 15:4 Comment(2)
try changing id to R.id.progressBar2Wardwarde
Thanks for the suggestion, as @Illegal Argument's answer I was using getActivity instead of viewInflatedConcordia
A
7

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);
Attraction answered 21/6, 2014 at 15:14 Comment(3)
Yep that was it...in fact I tried just changing the id of the fragment's ProgressBar which ended up in an exception, which makes sense as I was getting the Progressbar from the Activity not the FragmentConcordia
@Concordia Initially I thought that too because spinner is a dropdown like component and you named your progressbar variable spinner. A quick check of your code made me realize that the instantiation was incorrectAttraction
@Concordia it doesn't matter if you call Activity.findViewById or View.findViewById, try both and see what those methods return, if they return the same View (and they will if you use different ids) the result will always be the sameWardwarde
C
6

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);
    }
});
Claman answered 5/12, 2016 at 9:36 Comment(0)
O
1

It's probably due the fact that View.GONE will prevent the View from being drawn to the screen, while another View android:layout_align[POSITION] component may be defined to that View, so their position can't be calculated.

View.INVISIBLE will work cause it just make it invisible, but the View is still there and other View can calculate their position if the align is set to it

Oatcake answered 21/6, 2014 at 15:9 Comment(1)
Thanks, just realised I was getting the ProgressBar from the Activity not the FragmentConcordia
U
0

This work for me:

rootView.findViewById(R.id.progress_bar).setVisibility(View.GONE);
Unsaddle answered 22/9, 2014 at 8:14 Comment(0)
K
-1

progressBar.setVisibility(ProgressBar.VISIBLE);

Keef answered 28/9, 2015 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.