• Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp" android:id="@+id/relativeLayout" tools:context=".MainActivity"> <Switch android:id="@+id/switchBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="ON" android:thumb="@drawable/customswitchselector" android:track="@drawable/custom_track" android:layout_centerInParent="true" android:textOff="OFF"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/switchBtn" android:layout_marginTop="20dp" android:text="Switch ON and OFF" android:textSize="24sp" android:textAlignment="center" android:textStyle="bold"/> </RelativeLayout>
• Create a drawable resource file (customswitchselector.xml) and add the following code −
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true"> <corners android:radius="15dp" /> <gradient android:angle="270" android:endColor="#6600FF00" android:startColor="#66AAFF00" /> <size android:width="37dp" android:height="37dp" /> <stroke android:width="4dp" android:color="#0000ffff" /> </shape> </item> <item android:state_checked="false"> <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true"> <corners android:radius="15dp" /> <gradient android:angle="270" android:endColor="#ff0000" android:startColor="#ff0000" /> <size android:width="37dp" android:height="37dp" /> <stroke android:width="4dp" android:color="#0000ffff" /> </shape> </item> </selector>
• Create a drawable resource file (custom_track.xml) and add the following code −
<?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="#27170432" android:endColor="#27170432" android:angle="270"/> <corners android:radius="15dp"/> <size android:width="80dp" android:height="40dp" /> </shape>
• Add the following code to src/MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Switch aSwitch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); aSwitch = findViewById(R.id.switchBtn); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(MainActivity.this, "Switch On", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Switch Off", Toast.LENGTH_SHORT).show(); } } }); } }
• Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>