stramin
stramin

Reputation: 2390

How to change Android Toggle/LabeledSwitch border color programmatically?

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)

Upvotes: 0

Views: 1198

Answers (1)

DemoDemo
DemoDemo

Reputation: 392

  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.

Upvotes: 1

Related Questions