1

I have a SeekBar in my app and I want to change the colors of thumb and progressbar programmatically in my MainActivity

I know how to do it in XML:

android:thumbTint="@color/myColorTest"
android:progressDrawable="@color/myColorTest2"

But I want to implement this on SeekBar - onProgressChanged - to be able to change colors in realtime. I found some examples on how to do it - like this one - but they are all in Java (and I think they are deprecated).

I'm struggling on finding out the correct syntax in Kotlin for it.

Thank you!

Following David Kroukamp advice I run into this:

seekbar.getProgressDrawable().colorFilter(Color.RED, PorterDuff.Mode.SRC_IN)
seekbarr.getThumb().colorFilter(Color.RED, PorterDuff.Mode.SRC_IN)

Where colorFilter is in red warning "unresolved reference" colorFilter unresolver reference

mvqdev
  • 31
  • 6
  • 1
    Just an FYI pasting Java code into a kotlin file it will prompt for the code to be converted, in Android Studio at least. Also have you tried using intellisense to see what fields are available in the seekbar variable and if any match `seekbar.thumbTint` or `seekbar.progressDrawable` – David Kroukamp Dec 21 '20 at 15:13
  • Thank you for the answer! I did not know about the Java-pasting-coverted-code-sugestion. That's cool. I did tried using intellisense and pretty sure it's pointing me in the right directions but I guess I'm too noob to understand. There are some methods that are supposed to be called next and I'm mixing it all – mvqdev Dec 21 '20 at 15:58
  • Glad to help. Try `seekbar.progressDrawable.setColorFilter(...)` and `seekbar.thumb.setColorFilter(...)` it also helps in these situations to read the documentation on what methods etc are available https://developer.android.com/reference/kotlin/android/widget/ProgressBar – David Kroukamp Dec 21 '20 at 16:38
  • You pushed me in the right direction ;) But now fighting a new situation... since colorFilter was deprecated: "This method was deprecated in API level 29. use setColorFilter(android.graphics.ColorFilter) with an instance of BlendModeColorFilter" Found this blog entry but this looks way to much lines just to change a color... If this is the solution I dont think my App really needs it. https://throwexceptions.com/android-setcolorfilter-is-deprecated-on-api29-throwexceptions.html – mvqdev Dec 21 '20 at 22:55
  • Hmmm you should be able to just use that first code snippet: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { seekbar.progressDrawable.setColorFilter(new BlendModeColorFilter(Color.RED, BlendMode.SRC_IN)); } else { seekbar.progressDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); }` – David Kroukamp Dec 22 '20 at 06:04
  • 1
    @DavidKroukamp between you and what I looked for got with a solution: **if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { mySeekBar.thumb.setTint(ContextCompat.getColor(this@MainActivity, R.color.passStrength_color_weak)) mySeekBar.progressDrawable.setTint(ContextCompat.getColor(this@MainActivity, R.color.passStrength_color_weak)) }** Was missing setTint... and had to add that if statemet checking for build version. It works for what I need but.. Is it good code? – mvqdev Dec 22 '20 at 15:35
  • Yay glad you got it working. I can't see anything wrong with the code you have! Nice work! A fresh change from other people who just want to be spoon fed every single letter of code, you have the spirit of a true developer! – David Kroukamp Dec 22 '20 at 15:39
  • 1
    Thank you for those words David :) – mvqdev Dec 22 '20 at 15:46

1 Answers1

1

Go this to work for me:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
mySeekBar.thumb.setTint(ContextCompat.getColor(this@MainActivity, R.color.myTestColor))
mySeekBar.progressDrawable.setTint(ContextCompat.getColor(this@MainActivity, R.color.myTestColor))
}

setTint was what I needed and, to all work, I had to put it inside and if statement that checks for Build Version.

Even so, I'm not 100% convinced of this solution.. not sure if I'm checking for build version correctly and I'm doing with no else solution. So it means that in older versions I just "do" nothing.

EDITED: Following David suggestion, I deleted the if statement and tested the code. It compiled and seems to work correctly. The last thing, I was getting setTin in "red warning" by Android Studio. One suggested solution was to suppress it and is what I did.

mvqdev
  • 31
  • 6
  • Yes having no else does mean that, but you have to look at what is your minimum supported version? If it's greater then lollipop you don't don't need the if statement. And don't fall into the trap of trying to support the oldest android version, trust me these days not many phones have versions older then lollipop but it all depends on your country, what the app is being used for etc. Android studio shows the phone percentages according to version version you are creating a project and selecting the api level – David Kroukamp Dec 22 '20 at 15:40
  • In fact you can delete the if statement I'm sure android studio warns you if it's needed (if the call you are making is only supported from a virtual version) from what I remember – David Kroukamp Dec 22 '20 at 15:43
  • 1
    Humm yes... That's the way Android Studio suggested me. If not, setTint would be on "red" not allowing code to be compiled (only read now all comment where you mention "not falling the trap". And yes, that makes sense!) – mvqdev Dec 22 '20 at 15:51
  • 1
    well, just deleted that if statement and tested and code still compiles and works.. Though I still have my setTint on "red", compiler ignores it. To avoid staying with setTint on "red" Android Studio suggests (and is what I did) to suppres it – mvqdev Dec 22 '20 at 16:07
  • Then keep it as such, confirm what is your minimum sdk api in your build.gradle if it is less then lollipop you probably need the else if not carry on – David Kroukamp Dec 22 '20 at 16:24
  • Yes!! Thank you again David :) – mvqdev Dec 22 '20 at 16:27