javaandroidprogress-barandroid-progressbar

Unable to set progress to progress bar programatically


Hello Everyone Im facing issue with progress bar progress, I’m trying to update the progress on my progress bar, but it consistently shows values of 100% or higher for some unknown reason.

Here is what I'm experiencing

Below are the relevant XML and Java code snippets:



        <!-- Other Code -->
        <ProgressBar
            android:id="@+id/overall_score_progress_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_gravity="bottom"
            android:max="100"
            android:layout_marginBottom="35dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp" />
            
 
public class FragmentResults extends Fragment {
    ProgressBar overallScoreProgressBar;
    int overall;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_results, container, false);

        // Initialize ProgressBars
        overallScoreProgressBar = view.findViewById(R.id.overall_score_progress_bar);
       
        // Generate random scores
        generateRandomScores();

        // Set progress bar colors based on scores
        setProgressBarColor(overallScoreProgressBar, overall);
        
        return view;
    }

    // Method to calculate color based on progress
    private void setProgressBarColor(ProgressBar progressBar, int progress) {
        int color = getColorFromProgress(progress);
        progressBar.getProgressDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
    }

    // Method to determine the color based on the progress percentage
    private int getColorFromProgress(int progress) {
        if (progress <= 20) {
            return interpolateColor(Color.parseColor("#CC0000"), Color.parseColor("#FF6666"), progress / 20.0f);
        } else if (progress <= 40) {
            return interpolateColor(Color.parseColor("#E68A00"), Color.parseColor("#FFB266"), (progress - 20) / 20.0f);
        } else if (progress <= 60) {
            return interpolateColor(Color.parseColor("#CCCC00"), Color.parseColor("#FFFF66"), (progress - 40) / 20.0f);
        } else if (progress <= 80) {
            return interpolateColor(Color.parseColor("#009900"), Color.parseColor("#66FF66"), (progress - 60) / 20.0f);
        } else {
            return interpolateColor(Color.parseColor("#33CC33"), Color.parseColor("#99FF99"), (progress - 80) / 20.0f);
        }
    }

    // Helper method to interpolate between two colors
    private int interpolateColor(int colorStart, int colorEnd, float ratio) {
        int rStart = (colorStart >> 16) & 0xFF;
        int gStart = (colorStart >> 8) & 0xFF;
        int bStart = colorStart & 0xFF;

        int rEnd = (colorEnd >> 16) & 0xFF;
        int gEnd = (colorEnd >> 8) & 0xFF;
        int bEnd = colorEnd & 0xFF;

        int r = (int) (rStart + (rEnd - rStart) * ratio);
        int g = (int) (gStart + (gEnd - gStart) * ratio);
        int b = (int) (bStart + (bEnd - bStart) * ratio);

        return Color.rgb(r, g, b);
    }

    private void generateRandomScores() {
        Random random = new Random();

        // Generate random scores between 0 and 100
        overall = random.nextInt(101);
       
        // Other score generations

        // Set scores to TextViews and progress bars
        setScoresAndProgress();
    }

    private void setScoresAndProgress() {
        // Set the scores to TextViews
        overallScore.setText(String.valueOf(overall));
        
        // Set the progress bars
        overallScoreProgressBar.setProgress(overall);
    }
}

I would greatly appreciate any solutions to resolve this issue!

EDIT:- As requested by @Kaung Khant Kyaw im sharing the entire code

public class FragmentResults extends Fragment {

     TextView overallScore, potentialScore, masculinityScore, jawlineScore,skinScore, cheekboneScore, eyesScore, hairScore;
     ProgressBar overallScoreProgressBar, potentialScoreProgressBar, masculinityScoreProgressBar,
           skinScoreProgressBar, jawlineScoreProgressBar, cheekboneScoreProgressBar, eyesScoreProgressBar, hairScoreProgressBar;

    SeekBar seekBar;
   
     int overall,potential,skin,masculinity,jawline,cheekbone,eyes,hair;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_results, container, false);
        // Initialize TextViews
        overallScore = view.findViewById(R.id.overall_score);
        potentialScore = view.findViewById(R.id.potential_score);
        masculinityScore = view.findViewById(R.id.masculinity_score);
        jawlineScore = view.findViewById(R.id.jawline_score);
        cheekboneScore = view.findViewById(R.id.cheekbone_score);
        eyesScore = view.findViewById(R.id.eyes_score);
        hairScore = view.findViewById(R.id.hair_score);
        skinScore = view.findViewById(R.id.skin_score);

        // Initialize ProgressBars
        overallScoreProgressBar = view.findViewById(R.id.overall_score_progress_bar);
        potentialScoreProgressBar = view.findViewById(R.id.potential_score_progress_bar);
        masculinityScoreProgressBar = view.findViewById(R.id.masculinity_score_progress_bar);
        jawlineScoreProgressBar = view.findViewById(R.id.jawline_score_progress_bar);
        cheekboneScoreProgressBar = view.findViewById(R.id.cheekbone_score_progress_bar);
        eyesScoreProgressBar = view.findViewById(R.id.eyes_score_progress_bar);
        hairScoreProgressBar = view.findViewById(R.id.hair_score_progress_bar);
        skinScoreProgressBar= view.findViewById(R.id.skin_score_progress_bar);

        // Generate random scores
        generateRandomScores();





        // Set progress bar colors based on scores
        setProgressBarColor(overallScoreProgressBar, overall);
        setProgressBarColor(potentialScoreProgressBar, potential);
        setProgressBarColor(masculinityScoreProgressBar, masculinity);
        setProgressBarColor(jawlineScoreProgressBar, jawline);
        setProgressBarColor(cheekboneScoreProgressBar, cheekbone);
        setProgressBarColor(eyesScoreProgressBar, eyes);
        setProgressBarColor(hairScoreProgressBar, hair);
        setProgressBarColor(skinScoreProgressBar,skin);



        seekBar = view.findViewById(R.id.hotnessscoreProgressBar);
        // Create a Bitmap with the fire emoji
        Paint paint = new Paint();
        paint.setTextSize(60); // Adjust the size for the emoji
        paint.setColor(Color.BLACK);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true); // Ensures smooth edges

        // Create a Bitmap with transparent background (ARGB_8888 supports transparency)
        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        // Clear the canvas with transparent color
        canvas.drawColor(Color.TRANSPARENT);

        // Draw the emoji onto the canvas, centered
        canvas.drawText("🔥", 50, 75, paint);

        // Create a BitmapDrawable from the Bitmap
        BitmapDrawable thumbDrawable = new BitmapDrawable(getResources(), bitmap);

        // Set the bounds for the thumb drawable (adjust as needed)
        thumbDrawable.setBounds(0, 0, 100, 100);

        // Apply the thumb drawable to the SeekBar
        seekBar.setThumb(thumbDrawable);

        return view;
    }

    // Method to calculate color based on progress
    private void setProgressBarColor(ProgressBar progressBar, int progress) {
        int color = getColorFromProgress(progress);
        progressBar.getProgressDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
    }

    // Method to determine the color based on the progress percentage
    private int getColorFromProgress(int progress) {
        if (progress <= 20) {
            return interpolateColor(Color.parseColor("#CC0000"), Color.parseColor("#FF6666"), progress / 20.0f);
        } else if (progress <= 40) {
            return interpolateColor(Color.parseColor("#E68A00"), Color.parseColor("#FFB266"), (progress - 20) / 20.0f);
        } else if (progress <= 60) {
            return interpolateColor(Color.parseColor("#CCCC00"), Color.parseColor("#FFFF66"), (progress - 40) / 20.0f);
        } else if (progress <= 80) {
            return interpolateColor(Color.parseColor("#009900"), Color.parseColor("#66FF66"), (progress - 60) / 20.0f);
        } else {
            return interpolateColor(Color.parseColor("#33CC33"), Color.parseColor("#99FF99"), (progress - 80) / 20.0f);
        }
    }

    // Helper method to interpolate between two colors
    private int interpolateColor(int colorStart, int colorEnd, float ratio) {
        int rStart = (colorStart >> 16) & 0xFF;
        int gStart = (colorStart >> 8) & 0xFF;
        int bStart = colorStart & 0xFF;

        int rEnd = (colorEnd >> 16) & 0xFF;
        int gEnd = (colorEnd >> 8) & 0xFF;
        int bEnd = colorEnd & 0xFF;

        int r = (int) (rStart + (rEnd - rStart) * ratio);
        int g = (int) (gStart + (gEnd - gStart) * ratio);
        int b = (int) (bStart + (bEnd - bStart) * ratio);

        return Color.rgb(r, g, b);
    }
    private void generateRandomScores() {
        Random random = new Random();

        // Generate random scores between 0 and 100
        overall = random.nextInt(101);
        potential = random.nextInt(101);
        masculinity = random.nextInt(101);
        jawline = random.nextInt(101);
        cheekbone = random.nextInt(101);
        skin = random.nextInt(101);
        eyes = random.nextInt(101);
        hair = random.nextInt(101);

        // Set scores to TextViews and progress bars
        setScoresAndProgress();
    }

    private void setScoresAndProgress() {
        // Set the scores to TextViews
        overallScore.setText(String.valueOf(overall));
        potentialScore.setText(String.valueOf(potential));
        masculinityScore.setText(String.valueOf(masculinity));
        jawlineScore.setText(String.valueOf(jawline));
        cheekboneScore.setText(String.valueOf(cheekbone));
        eyesScore.setText(String.valueOf(eyes));
        hairScore.setText(String.valueOf(hair));
        skinScore.setText(String.valueOf(skin));

        // Set the progress bars
        overallScoreProgressBar.setProgress(overall);
        potentialScoreProgressBar.setProgress(potential);
        masculinityScoreProgressBar.setProgress(masculinity);
        jawlineScoreProgressBar.setProgress(jawline);
        cheekboneScoreProgressBar.setProgress(cheekbone);
        eyesScoreProgressBar.setProgress(eyes);
        hairScoreProgressBar.setProgress(hair);
        skinScoreProgressBar.setProgress(skin);
    }

}

Solution

  • The function getProgressDrawable().setColorFilter() sets the color of the progressBar not the progress.

    Here's an approach to change the color only the progress amount.

    val progressBar = findViewById<ProgressBar>(R.id.progressBar)
    
    // Retrieve the LayerDrawable from the progress bar
    val progressDrawable = progressBar.progressDrawable as LayerDrawable
    

    Then use this progressDrawable and change its color.

    private void setProgressBarColor(ProgressBar progressBar, int progress) {
    
           int color = getColorFromProgress(progress);
           val progressLayer = progressDrawable.findDrawableByLayerId(android.R.id.progress)
    
           // Set the desired color for the progress (not affecting the background)
           val color = ContextCompat.getColor(this, R.color.your_color) 
           progressLayer.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
        }