tommybuonomo / dotsindicator

Three material Dots Indicators for view pagers in Android !
Apache License 2.0
3.49k stars 356 forks source link

Changing dots color individually #193

Open ugurkuyu opened 9 months ago

ugurkuyu commented 9 months ago

Can we change the color of each dot individually like this? I couldn't find a way to do it.

Ekran Resmi 2024-02-16 15 13 24
ccojocea commented 3 months ago

Hey, would also be interested in changing color of each dot individually (using this as xml with Java). Specifically would be interested in changing the unselected color and selected color of any single dot position. Thanks.

For anyone interested, I was able to modify the color of a specific item through my Java code like this. If you have different colors for selected/unselected, you'll need to call this in the registered viewpager listener as well as calling it after attaching it.

    private void modifyDotColor(int position, int colorRes) {
        try {
            // Access the dot (ImageView)
            ViewGroup baseViewGroup = (ViewGroup) dotsIndicator.getChildAt(0);
            ViewGroup dotViewGroup = (ViewGroup) baseViewGroup.getChildAt(position);
            View dot = dotViewGroup.getChildAt(0);
            if (dot instanceof ImageView) {
                ImageView lastDotImageView = (ImageView) dot;
                Drawable drawable = lastDotImageView.getBackground(); //DotsGradientDrawable but not needed
                ColorFilter colorFilter = new BlendModeColorFilter(
                        ContextCompat.getColor(VehicleStatusActivity.this, colorRes),
                        BlendMode.SRC_IN
                );
                drawable.setColorFilter(colorFilter);
                lastDotImageView.setBackground(drawable);
            }
        } catch (NullPointerException nullPointerException) {
            Logger.error(getClass(), nullPointerException.getMessage(), nullPointerException);
        }
    }