Emanuele Papa
Emanuele Papa

Reputation: 43

Android ProgressBar setProgress() doesn't work as intended

I'm working on a progress bar that shows the rating of a game. Here an example CorrectGameRatingProgressBar.

Here's the code

  ProgressBar userRatingProgressBar = root.findViewById(R.id.users_rating);
  userRatingProgressBar.setProgress(0);
  ...
  // game ratings
  if (game.getRatingCount() > 0) {
      userRatingProgressBar.setProgress((int) game.getRating());
      gameUsersRatingText.setText(String.valueOf((int) game.getRating()));
  } else {
      userRatingProgressBar.setProgress(0);
      gameUsersRatingText.setText("N/A");
  }
  ...

It works well except for a thing: sometimes seems that setProgress(0) doesn't update the progress bar when I switch from a game to another that doesn't have a rating as shown here IncorrectGameRatingProgressBar. I even tried to set the progress bar to 0 before the "null check" but this thing happens anyway.

I tried this on a Xiaomi Mi9T with MIUI 12.0.5 (Android 10 QKQ1.190825.002) and on the Pixel 2 emulated on Android Studio with Android 11

Is there a way to fix this problem? If you need more infos, don't bother asking!
Thank you for all


I leave you down here the layout layout.xml of the activity

 <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/user_rating_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="?attr/GameDetailTextColor"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/users_rating"
                    app:layout_constraintEnd_toEndOf="@+id/users_rating"
                    app:layout_constraintStart_toStartOf="@+id/users_rating"
                    app:layout_constraintTop_toTopOf="@+id/users_rating"
                    tools:text="60" />

                <ProgressBar
                    android:id="@+id/users_rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:indeterminateOnly="false"
                    android:progressDrawable="@drawable/rating_circle"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:progress="60" />

            </androidx.constraintlayout.widget.ConstraintLayout>

and the drawable custom_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="ring"
            android:thicknessRatio="16"
            android:useLevel="false">
            <solid android:color="?attr/SearchBarBackgroundColor" />
        </shape>
    </item>
    <item>
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270">
            <shape
                android:innerRadiusRatio="3.1"
                android:shape="ring"
                android:thicknessRatio="12"
                android:useLevel="true">
                <solid android:color="@color/orange_700" />
            </shape>
        </rotate>
    </item>
</layer-list>

EDIT: here's the code of the java fragment:

public class GameDetailFragment extends Fragment {
    ...
    private long gameId = 0;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_game_detail, container, false);
        if (getArguments() != null) {
            gameId = getArguments().getLong("GAME_ID");
        }
    GameDetailViewModelFactory viewModelFactory = ((MainActivity) requireActivity()).getAppContainer().gameDetailViewModelFactory;
    mViewModel = new ViewModelProvider(this, viewModelFactory).get(GameDetailViewModel.class);
    ...
    ProgressBar userRatingProgressBar = root.findViewById(R.id.users_rating);
    userRatingProgressBar.setProgress(0);
    if (gameId != 0) {
    ...
    // game ratings
                if (game.getRatingCount() > 0) {
                    userRatingProgressBar.setProgress((int) game.getRating());
                    gameUsersRatingText.setText(String.valueOf((int) game.getRating()));
                } else {
                    userRatingProgressBar.setProgress(0);
                    gameUsersRatingText.setText("N/A");
                }
    ...
    return root;
    }
}

Upvotes: 1

Views: 1852

Answers (1)

Emanuele Papa
Emanuele Papa

Reputation: 43

I find a solution! Apparently I didn't set up the progress bar into the layout.xml
So, if you add android:progress="1" in the xml, the progress bar is getting updated everytime.

Upvotes: 1

Related Questions