'Why is my progress bar not turning visible in Android Studio
I am attempting to build an app that displays a "ladder" between two different words of the same length. While my program is finding the ladder between these two words, I'd like to display a progress bar to give the illusion of a the app loading. However, whenever I try to change the progressBar's visibility from "invisible" to "visible", it never appears (although it does appear if I just leave the progresBar's visibility as "visible"), and I am attempting to figure out why. I've read several other forum posts that as similar questions to mine, but none of their solutions are working for me.
Here is the code in my BuilderActivity.java file, which takes the two words, finds the ladder between them, and displays it to the screen:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
public class BuilderActivity extends AppCompatActivity {
ProgressBar pb = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.builder_activity);
}
public void handleText(View v){
pb = (ProgressBar) findViewById(R.id.progressBar);
if(pb.getVisibility() == View.INVISIBLE) {
pb.setVisibility(View.VISIBLE);
}
TextView start = findViewById(R.id.startingWord);//Gets the starting word input from the application
String startInput = start.getText().toString();
TextView end = findViewById(R.id.endingWord);//Gets the ending word input from the application
String endInput = end.getText().toString();
//generateLadder(startInput, endInput);
buildLadder(startInput, endInput);
}
public void buildLadder(String startingWord, String endingWord) {
InputStream is = getFile("dictionary");
DictionaryBuilder d1 = new DictionaryBuilder(is);
Collection<String> listOfWordsLength = null;
try {
listOfWordsLength = d1.getWordsOfLength(startingWord.length());
} catch (IOException ex) {
ex.getMessage();
}
LadderBuilder ladder = new LadderBuilder(listOfWordsLength);
try {
Deque<String> finalLadder = ladder.buildLadder(startingWord, endingWord);
if (finalLadder == null) {//There is no ladder between the two words
textToScreen("Error: There is no link between the words " + startingWord + " and "
+ endingWord + ".");
} else {
textToScreen(finalLadder.toString());
}
}catch (IllegalArgumentException ex){//The words are not of the same length.
textToScreen("Error: The words " + startingWord + " and " + endingWord + " are not "
+ "the same length.");
}
}
public void textToScreen(String text){
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
pb = (ProgressBar) findViewById(R.id.progressBar);
pb.setVisibility(View.INVISIBLE);
textView.setVisibility(View.VISIBLE);
}
private InputStream getFile(String fileName){
InputStream file = null;
try {
file = getAssets().open(fileName);
}catch (IOException ex) {
ex.getMessage();
}
return file;
}
}
And here is my XLM code for the builder activity:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
style="@style/layout"
tools:context=".BuilderActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="55dp"
android:layout_height="59dp"
android:secondaryProgress="100"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.116" />
<Button
android:id="@+id/generateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="148dp"
android:onClick="handleText"
android:text="Generate Ladder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/startingWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="108dp"
android:ems="10"
android:inputType="textPersonName"
android:text="stack"
app:layout_constraintBottom_toTopOf="@+id/generateButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/endingWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="stave"
app:layout_constraintBottom_toTopOf="@+id/generateButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/startingWord"
app:layout_constraintVertical_bias="0.349" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="TextView"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/startingWord"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.258" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Solution 1:[1]
I tried running your code. it is working fine for me (progress bar showing). possible fix
Rebuild gradle project
Clean project and rebuild instead
and change this code:-
- find progress bar view in onCreate
- use
pb.getVisibility()!=View.VISIBLE
in if condition
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (ProgressBar) findViewById(R.id.progressBar);
}
public void handleText(View v){
if(pb.getVisibility() != View.VISIBLE) {
pb.setVisibility(View.VISIBLE);
}
}
}
if nothing works then it must be the glitch of android studio. other possible fixes i do in this kind of situation:-
- Try changing progress bar's id in xml
- Try changing width, height or styles
- Finally create a new activity or app and copy the code in that
this things can help a lot when problem is related to IDE
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |