In the below example I have created Customizing Switch button. Here I have added a Switch in activity_main.xml layout. In second step I have created backgraund.xml layout in drawable folder, here I have design switch backgraund shape. In third step I have created a new customtrack.xml layout in drawable folder.Now in next step I have added string name with in String.xml layout. In MainActivity I have used toast function. You can see below program it clearly describe you How to make Custom Switch in android.
Step(1)activity_main.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgraund"
android:orientation="vertical">
<Switch android:id="@+id/swtch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="@string/checkedstate"
android:textOff="@string/uncheckedstate"
android:minWidth="25dp"
android:layout_marginTop="29dp"
android:layout_marginLeft="40dp"
android:switchPadding="10dp"
android:onClick="togglestatehandler"
android:thumb="@drawable/backgraund"
android:track="@drawable/customtrack"
android:thumbTextPadding="15dp"
android:textStyle="bold|italic"
android:typeface="sans"/>
</LinearLayout>
Step(2)-Created a new backgraund.xml layout in drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true">
<shape android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false">
<gradient android:startColor="#66AAFF00"
android:endColor="#6600FF00"
android:angle="270"/>
<corners android:radius="15dp"/>
<size android:width="27dp"
android:height="37dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false">
<gradient android:startColor="#66e133b6"
android:endColor="#6619e4d6"
android:angle="270"/>
<corners android:radius="15dp"/>
<size android:width="27dp"
android:height="37dp" />
</shape>
</item>
</selector>
Step(3)-Created a new customtrack.xml layout in drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false">
<gradient android:startColor="#660000FF"
android:endColor="#6600CCFF"
android:angle="270"/>
<corners android:radius="15dp"/>
<size android:width="30dp"
android:height="40dp" />
</shape>
Step(4)-Add string in string.xml -
<resources>
<string name="checkedstate">START</string>
<string name="uncheckedstate">STOP</string>
</resources>
Step(5)-MainActivity-
public class MainActivity extends AppCompatActivity {
Switch switchbtn;
private Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchbtn = (Switch) findViewById(R.id.swtch);
applyStyle(switchbtn.getTextOn(), switchbtn.getTextOff());
}
public void applyStyle(CharSequence switchTxtOn, CharSequence switchTxtOff) {
Spannable styleText = new SpannableString(switchTxtOn);
StyleSpan style = new StyleSpan(Typeface.BOLD);
styleText.setSpan(style, 0, switchTxtOn.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
styleText.setSpan(new ForegroundColorSpan(Color.GREEN), 0, switchTxtOn.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
switchbtn.setTextOn(styleText);
styleText = new SpannableString(switchTxtOff);
styleText.setSpan(style, 0, switchTxtOff.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
styleText.setSpan(new ForegroundColorSpan(Color.RED), 0, switchTxtOff.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
switchbtn.setTextOff(styleText);
}
public void togglestatehandler(View v) {
Switch switchbtn = (Switch) v;
boolean isChecked = switchbtn.isChecked();
if (isChecked) {
Toast.makeText(context, "On......", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Off......", Toast.LENGTH_SHORT).show();
}
}
}
0 Comment(s)