简体   繁体   中英

setVisibility on ProgressBar in Android

I have the following progress bar in my Android application:

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:layout_marginTop="60sp"
        android:visibility="gone"
        android:indeterminate="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

I also have an ImageView with an image in it. What I want to do is while I'm making a call to an API, make the image invisible and show the ProgressBar in its place, then when it ends show the image again and hide the ProgressBar. However, this does not work:

searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressBar progressBar = findViewById(R.id.progressBar);
                ImageView image = findViewById(R.id.image);

                image.setVisibility(View.INVISIBLE);
                progressBar.setVisibility(View.VISIBLE);

                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                         makeRequest();
                    }
                });
                thread.start();

                try {
                    thread.join();
                    progressBar.setVisibility(View.INVISIBLE);
                    image.setVisibility(View.VISIBLE);
                } catch (InterruptedException e) {
                    
                }
            }
        });

I've tried various combinations with View.INVISIBLE and View.GONE, nothing works. I've also searched through similar questions on here but none of them answers my question.

I thought maybe it's because it disappears and reappears so quickly I don't notice it, but even if I do Thread.sleep() the progress bar still doesn't show up.

What am I doing wrong?

Calling thread.join() within OnClickListener blocks the UI thread and hence block any UI updates.

To fix your code, you can remove thread.join() and put progressBar.setVisibility(View.INVISIBLE);image.setVisibility(View.VISIBLE); within run() after makeRequest() but warp with runOnUiThread()

searchBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ProgressBar progressBar = findViewById(R.id.progressBar);
        ImageView image = findViewById(R.id.image);

        image.setVisibility(View.INVISIBLE);
        progressBar.setVisibility(View.VISIBLE);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                makeRequest();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        progressBar.setVisibility(View.INVISIBLE);
                        image.setVisibility(View.VISIBLE);            
                    }
                });
            }
        });
        thread.start();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM