0

I would like to change the style for one switch button

This is the xml code,

<Switch
 android:id="@+id/switch
 style="@style/switchStyle"
 android:layout_alignParentRight="true"
/>

but I want to do this with code:

Switch newSwitch = new Switch(context);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL); 

viewHolder.switchButton = newSwitch;       
viewHolder.switchButton.setLayoutParams(params);
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56

1 Answers1

0

If you try to set params, you need to set the style again.

this might be useful : Android: set view style programmatically

Community
  • 1
  • 1