[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Cool Switch View

The provided code defines a method to create and configure a Switch component in an Android application. It customizes the appearance of the switch based on its state and saves the state in user preferences. Additionally, it handles state changes to update preferences and layout accordingly.

Uploaded by

enopng2670
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Cool Switch View

The provided code defines a method to create and configure a Switch component in an Android application. It customizes the appearance of the switch based on its state and saves the state in user preferences. Additionally, it handles state changes to update preferences and layout accordingly.

Uploaded by

enopng2670
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

private void Switch(LinearLayout linLayout, final int featNum, final String

featName, boolean swiOn) {


final Switch switchR = new Switch(getContext);
final GradientDrawable THUMB = new GradientDrawable();
THUMB.setShape(1);
THUMB.setSize(40,40);

final GradientDrawable TRACK = new GradientDrawable();


TRACK.setCornerRadius(100);
TRACK.setSize(20,20);

boolean isOn = Preferences.loadPrefBool(featName, featNum, swiOn);


if(isOn){
THUMB.setColor(BTN_COLOR);
THUMB.setStroke(2,BTN_COLOR);

TRACK.setColor(Color.parseColor("#AB143568"));
TRACK.setStroke(2, BTN_COLOR);
}
else{
THUMB.setColor(Color.WHITE);
THUMB.setStroke(2,BTN_COLOR);

TRACK.setColor(Color.argb(200,255,255,255));
TRACK.setStroke(2, BTN_COLOR);
}

switchR.setText(featName);
switchR.setTextColor(TEXT_COLOR_2);
switchR.setThumbDrawable(THUMB);
switchR.setTrackDrawable(TRACK);
switchR.setPadding(10, 5, 0, 5);
switchR.setChecked(Preferences.loadPrefBool(featName, featNum, swiOn));
switchR.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton compoundButton, boolean
bool) {
Preferences.changeFeatureBool(featName, featNum, bool);
if(bool){
THUMB.setColor(BTN_COLOR);
THUMB.setStroke(2,BTN_COLOR);

TRACK.setColor(Color.parseColor("#AB143568"));
TRACK.setStroke(2, BTN_COLOR);
}
else{
THUMB.setColor(Color.WHITE);
THUMB.setStroke(2,BTN_COLOR);

TRACK.setColor(Color.argb(200,255,255,255));
TRACK.setStroke(2, BTN_COLOR);
}
switch (featNum) {
case -1: //Save perferences
Preferences.with(switchR.getContext()).writeBoolean(-1,
bool);
if (bool == false)
Preferences.with(switchR.getContext()).clear(); //Clear
perferences if switched off
break;
case -3:
Preferences.isExpanded = bool;
scrollView.setLayoutParams(bool ? scrlLLExpanded : scrlLL);
break;

}
}
});

linLayout.addView(switchR);
}

You might also like