0

I am using Android Toggle library by Angad Singh, to create a LabeledSwitch programmatically, 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 programmatically? (not xml)

Cœur
  • 37,241
  • 25
  • 195
  • 267
stramin
  • 2,183
  • 3
  • 29
  • 58
  • Which version of the lib do you use? It seems to work just fine on implementation 'com.github.angads25:toggle:1.1.0' – DemoDemo Mar 02 '20 at 20:08

1 Answers1

1
  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.

DemoDemo
  • 372
  • 1
  • 12
  • I increased version from `1.0.0` to `1.1.0` and used the method `setColorBorder()` (for Java) and it worked, thank you. – stramin Mar 03 '20 at 11:48