androidimagebuttontint

how to get ImageButton tint color and save it to sqlite in android studio? (Get tint value not set it)


Hi i have this ImageButton:

<ImageButton
            android:id="@+id/imgBtn20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_lens_50_white"
            android:tint="@color/color_picker_color_20"
            android:backgroundTint="@color/color_white"
            android:layout_weight="0.25" />

I want to save color_picker_color_20 value as a hex value like #FFFFFF in my sqlite database when user clicks on this imageButton. there is a lot of answers on how to set tint value. but i only want to get this tint value. How can I do this? any help would be useful thank you.


Solution

  • Try this:

    val tintColor: Int = imageButton.imageTintList?.defaultColor
    if (tintColor != null) {
        val hexColor = String.format("#%06X", 0xFFFFFF and tintColor)
        println("Color is: $hexColor")
    }
    

    Here, imageButton.imageTintList gives us a ColorStateList which can have different colors based on different states (e.g. pressed, selected, etc.)

    Since, you're only using a direct color-reference, it will be available as default color for default state. After we get this color, we can format it to show RGB color.