Reputation: 653
I have a problem with ProgressBar Widget.
This is the ProgressBar in the layout
...
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"
android:visibility="gone"
/>
</RelativeLayout>
....
This is the activity, where I have 2 methods.
....
private ProgressBar mProgressBar;
private Handler handler = new Handler();
private int i = 0;
...
protected void onCreate(Bundle savedInstanceState) {
...
mProgressBar = findViewById(R.id.progressBar);
...
}
private void showProgressBar() {
mProgressBar.setVisibility(View.VISIBLE);
}
private void hideProgressBar() {
mProgressBar.setVisibility(View.GONE);
}
The main activity have a Menu, on menu click I want to run a long task, so I want to show a progressBar
public boolean onOptionsItemSelected(MenuItem item) {
showProgressBar();
// This lines try to emulate a long task
i = mProgressBar.getProgress();
new Thread(new Runnable() {
public void run() {
while (i < 100) {
i += 1;
// Update the progress bar and display the current value in text view
handler.post(new Runnable() {
public void run() {
mProgressBar.setProgress(i);
//txtView.setText(i+"/"+pgsBar.getMax());
}
});
try {
// Sleep for 100 milliseconds to show the progress slowly.
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
The lines below the call to the function is a test, I'm trying to delay the show and let me see the progress, but nothing is shown.
If I change the layout and change: android:visibility="visible" or remove the line
when the activity start I can see the progress, and when a line with hideProgressBar() function, in the end of onOptionsItemSelected(), is reached, the widget is hide or gone.
Where is the problem ? Is about Threads ?
I'm running this app in a Virtual Device. I could't find some settings about showing animations, but because I can see the progress if the visibility is set to VISIBLE.. I suppose that is not the problem.
Best regards
Compile SDK: 28 Android Studio: 3.6.3
UPDATE: Full Layout Source
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/filial_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/lista_filial_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"
android:visibility="gone"
/>
</RelativeLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/filial_appbar"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:context=".segregantes.ui.listafilial.ListaFilialActivity" />
</RelativeLayout>
UPDATE 2: New Code.
i = mProgressBar.getProgress();
runOnUiThread(new Runnable() {
@Override
public void run() {
while (i < 100) {
i += 1;
mProgressBar.setProgress(i);
try {
// Sleep for 100 milliseconds to show the progress slowly.
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Upvotes: 0
Views: 2001
Reputation: 108
Please see the following code it shows horizontal bar:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:max="100"
android:progress="1"
android:visibility="visible" />
Java code for showing progress bar
private int progressStatus = 0;
// Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Reference link: https://www.journaldev.com/9629/android-progressbar-example
Upvotes: 0
Reputation: 23384
You need to do UI operations on main thread so try doing this while setting progress
runOnUiThread(new Runnable() {
public void run() {
mProgressBar.setProgress(i);
}
});
Upvotes: 1