How can I style an Android Switch widget?

AndroidApps/ApplicationsMobile Development

This example demonstrates how do I Style an Android Switch Widget in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − 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>

Step 3 − 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>

Step 4 − 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>

Step 5 − 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();
            }
         }
      });
   }
}

Step 6 − 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>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

raja
Published on 26-Nov-2019 06:40:08
Advertisements