1

I've a condition check to decide if the ProgressBar should appear or not. The layout looks like below where the ProgressBar is contained within a RelativeLayout:

<RelativeLayout
    android:id="@+id/someID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ProgressBar
        android:id="@+id/someProgressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        />

</RelativeLayout>

In the fragment :

 ProgressBar progressBar;

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup   container,
                         @Nullable Bundle savedInstanceState) {
 View root = inflater.inflate(R.layout.some_main_layout, container, false);
 progressBar =(ProgressBar) root.findViewById(R.id.someProgressBar);
 ....

}

and I programtically try to change the visibility of the progress bar with the setVisibility() function like:

   if(someCondition)
       progressBar.setVisibility(View.GONE);
   else
       progressBar.setVisibility(View.VISIBLE);

But the above never works. So I tried to make changes at the xml level by adding android:visibility="gone" which sets the initial visibility to GONE state, but I'm again lost on how to make the ProgressBar visibile.

I appreciate your thoughts on this.

Thanks.

vardhinisuresh27
  • 371
  • 2
  • 6
  • 18
  • 1
    It means you must check your condition. Does it satisfy? – Piyush Mar 14 '17 at 05:34
  • @Piyush : I've just given the basic structure of the code. I know for sure the condition works (through debugging). – vardhinisuresh27 Mar 14 '17 at 05:44
  • @vardhinisuresh27 just check if you are setting visibility of correct progress bar , you initialized your progressbar as `someProgressBar` and you are setting visibility to `progressBar` . Also try changing your progressbar as global variable and see. – Manohar Mar 14 '17 at 05:47
  • @Redman Thanks. I've edited the post. I just have one progress bar and it is declared as a global variable in my code. – vardhinisuresh27 Mar 14 '17 at 05:49
  • @vardhinisuresh27 your problem is solve or not?? – Dileep Patel Mar 14 '17 at 06:55

3 Answers3

4

you can try to this hope this can help you.

ProgressBar someProgressBar;
TextView txtSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtSend = (TextView) findViewById(R.id.txtSend);
    someProgressBar = (ProgressBar) findViewById(R.id.someProgressBar);
    someProgressBar.setVisibility(View.GONE);

    txtSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (someProgressBar.getVisibility() == View.GONE) {
                someProgressBar.setVisibility(View.VISIBLE);
            } else {
                someProgressBar.setVisibility(View.GONE);
            }
        }
    });
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
0

Replace your code with below,

if(someCondition)
   progressBar.setVisibility(View.INVISIBLE);
 else
   progressBar.setVisibility(View.VISIBLE);
Keyur Thumar
  • 608
  • 7
  • 19
0

The problem is you are using wrong id for progress bar.

ProgressBar someProgressBar=(ProgressBar) root.findViewById(R.id.someProgressBar);

   if(someCondition)
       someProgressBar.setVisibility(View.GONE);
   else
       someProgressBar.setVisibility(View.VISIBLE);

and change your xml code for progress bar.If you want by defaultprogress bar to hide,then keep it as it is,otherwise remove visibility from xml.

Android Geek
  • 8,956
  • 2
  • 21
  • 35