0

I want to change the color of the icon upon selection, I found that the library does not exist in its properties

enter image description here

Just what I want to change the icon color to white when selected

    <io.ghyeok.stickyswitch.widget.StickySwitch
    android:id="@+id/sticky_switch"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1"
    android:layout_marginHorizontal="20dp"
    app:ss_animationDuration="600"
    app:ss_iconPadding="10dp"
    app:ss_iconSize="22dp"
    app:ss_leftIcon="@drawable/ic_contact_s"
    app:ss_leftText="Contact"
    app:ss_rightIcon="@drawable/ic_hourglass_s"
    app:ss_rightText="Requests"
    app:ss_textVisibility="gone"
    app:ss_selectedTextSize="13sp"
    app:ss_sliderBackgroundColor="@color/white"
    app:ss_switchColor="?colorPrimaryTheme"
    app:ss_textColor="?colorPrimaryTheme"
    app:ss_textSize="12sp"
    app:ss_animationType="line"
            />
Manoj
  • 2,059
  • 3
  • 12
  • 24
matrix
  • 3
  • 1

1 Answers1

0

You will have to do it programmatically or using a selector.

Programatically

Set a listener and then change the icon based on that:

stickySwitch.setOnSelectedChangeListener(object : StickySwitch.OnSelectedChangeListener {
    override fun onSelectedChange(direction: StickySwitch.Direction, text: String) {
        if (direction == StickySwitch.Direction.RIGHT) {
            stickySwitch.setRightIcon(R.drawable.ic_hourglass_s_white)
            stickySwitch.setLeftIcon(R.drawable.ic_contact_s_white)
        } else {
            stickySwitch.setRightIcon(R.drawable.ic_hourglass_s)
            stickySwitch.setLeftIcon(R.drawable.ic_contact_s)        
        }    
    }
})

If the library can support KT then the setter are not necessary:

stickySwitch.rightIcon = R.drawable.ic_hourglass_s_white

But considering the setter is passing an R.drawable resource then maybe it has to be used.

The library does support setting the drawable directly, so you could apply a tint to the drawable instead

stickySwitch.setLeftIcon(drawable)

Drawables are mutable so if you keep or get the reference you could replace the logic above and instead of using a tint. This question should help you with the tint part:

Selector

For creating a selector to make the job, there are plenty of questions and answers on SO, this example should help you:

That would only work if the library is working like a regular switch, and for applying it:

app:ss_leftIcon="@drawable/your_custom_selector"

A selector handles the view state, so if is selected it would choose a drawable otherwise the other.

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • The first example and I have also tried the rest of the examples, but I found the first example more suitable for me – matrix Nov 26 '20 at 06:07
  • Also, the selector did not work for me, but I think it is better in performance – matrix Nov 26 '20 at 06:11