How to change Android Toggle/LabeledSwitch border color programatically?

stramin :

I am using Android Toggle library by Angad Singh, to create a LabeledSwitch programatically, everything works but I am not being able to color the border.

LabeledSwitch switch = new LabeledSwitch(context);
switch.setLayoutParams(lp);
switch.setColorDisabled(context.getResources().getColor(R.color.colorDisabled));
switch.setColorOn(context.getResources().getColor(R.color.colorPrimary));
switch.setLabelOn("Yep!");
switch.setLabelOff("Nope!");

The xml property is app:colorBorder, if I write this property I get this message:

enter image description here

'colorBorder' has private access in 'com.github.angads25.toogle.LabeledSwitch'

How to change LabeledSwitch border color programatically? (not xml)

DemoDemo :
  val view = v.findViewById<LinearLayout>(R.id.container)

    val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
    val switch1 = LabeledSwitch(activity)
    switch1.layoutParams = lp
    switch1.colorDisabled = ContextCompat.getColor(activity!!, R.color.colorAccent)
    switch1.colorOn = ContextCompat.getColor(activity!!, R.color.colorPrimary)
    switch1.labelOn = "Yep!"
    switch1.labelOff = "Nope!"
    switch1.colorBorder = ContextCompat.getColor(activity!!, android.R.color.black)
    view.addView(switch1)

First you need to use ContextCompat to get your color resource and not using resources.getColor()

Again it seems to work for me for the more recent version 1.1.0 maybe you need to update your dependency.

Edit

if you are using kotlin you need to use the assign char = instead using parenthesis. if you use java you need to use setColorBorder instead.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20925&siteId=1