Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress Bar can not be hidden

Tags:

android

I have been trying to add a progress bar to my app that shows up when login button is clicked.
My problem is that I can't seem to hide the progress bar.
I tried setting pBar.setVisibility(View.GONE); but it isn't working. Android Studio doesn't show any error. Progress bar simply stays visible always.

Also, at first everything was running perfectly, this problem came up after I added a navigation drawer activity in my application and I am sending an intent to the login activity through that.
Is sending intent cause of the problem?

activity_login.xml

This is how I have added progress bar in xml.

<ProgressBar
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/progressBarLogin"
    android:indeterminateTint="@color/colorPrimary"
    android:layout_centerInParent="true"/>   

LoginActivity.java

This is how i am trying to hide it

public class LoginActivity extends AppCompatActivity {

ProgressBar pBar;
Button login;

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

    pBar = findViewById(R.id.progressBarLogin);

    pBar.setVisibility(View.GONE); //THIS LINE OF CODE IS NOT WORKING

    login = findViewById(R.id.login);  
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    pBar.setVisibility(View.VISIBLE);
                }
            });

            try{
                //Background login tasks
            }catch(Exception e){
                pBar.setVisibility(View.GONE);
                e.printStackTrace();
            }
        }
    });
}

EDIT- For all those people who are pointing out about pBar.setVisibility(View.GONE); being in the catch block and hence not running, that piece of code is running perfectly fine. Problem is that when I try to hide pbar at the start, that line simply doesn't seem to work.

like image 961
Mayank_kr_verma Avatar asked Jan 20 '26 23:01

Mayank_kr_verma


1 Answers

Try adding below line in try block after completing your background task (i.e) moving to another activity,

pBar.setVisibility(View.GONE);

inside try block.

like image 127
Android Avatar answered Jan 27 '26 01:01

Android