In this article, we will learn about android Material Switch using Kotlin. We will go through various example that demonstrates how to use different attributes of Material Switch. For example,
In this article, we will get answer to questions like –
- What is Material Switch?
- Why should we consider Material Switch while designing ui for any app?
- What are possibilities using Material Switch while designing ui? etc.
Let’s have a quick demo of things we want to cover in this tutorial –
Output
Getting Started
We can define android Material Switch widget as below –
Android Switch is a two-state toggle widget that can select between two options (ON or OFF). The user can drag and drop thumb to select between two options or it can simply tap to toggle the state.
Now, how do we use Material Switch in android application ?
Creating New Project
At first, we will create an application.
So, follow steps below to create any android project in Kotlin –
Step | Description |
---|---|
1. | Open Android Studio (Ignore if already done). |
2. | Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next. |
3. | In next screen, select project name as SwitchMaterial. Then, fill other required details. |
4. | Then, clicking on Finish button creates new project. |
Newbie in Android ?
Some very important concepts (Recommended to learn before you move ahead)
Before we move ahead, we need to setup for viewBinding to access Android Material Switch Using Kotlin file without using findViewById() method.
Setup ViewBinding
Add viewBinding true in app/build.gradle file.
android { // OTHER CODE... buildFeatures { viewBinding true } }
Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) } }
Now, we can access view in Kotlin file without using findViewById() method.
Using Material Switch in Kotlin
Follow steps below to use Material Switch in newly created project –
- Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">SwitchMaterial</string> <string name="off">OFF</string> <string name="on">ON</string> </resources>
- Open res/layout/activity_main.xml file. Then, add below code in it –
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/switch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/off" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
In activity_main.xml file, we have defined Switch widget. Now, we will access this Switch using kotlin file in the application.
-
We can also access it in Kotlin File, MainActivity.kt, as below –
package com.tutorialwing.switchmaterial import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.tutorialwing.switchmaterial.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) setCheckedChangeListener() } private fun setCheckedChangeListener() { binding.switchID.setOnCheckedChangeListener { _, isChecked -> val msg = getString(if (isChecked) R.string.on else R.string.off) Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show() binding.switchID.text = msg } } }
In MainActivity.kt file, we have accessed Switch widget defined in xml file. Then, we have set a checked change listener to show a toast message whenever checked state is changed.
Now, run the application. We will get output as below –
Different Attributes of Material Switch in XML
Now, we will see how to use different attributes of Android Material Switch using Kotlin to customise it –
Set Id of Material Switch
Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of Material Switch using android:id attribute like below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" />
Here, we have set id of Material Switch as materialSwitch_ID using android:id=”” attribute. So, if we need to reference this Material Switch, we need to use this id – materialSwitch_ID.
Learn to Set ID of Material Switch Dynamically
Set Width of Material Switch
We use android:layout_width=”” attribute to set width of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" />
Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of Material Switch Dynamically
Set Height of Material Switch
We use android:layout_height=”” attribute to set height of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of Material Switch Dynamically
Set Padding of Material Switch
We use android:padding=”” attribute to set padding of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" />
Here, we have set padding of 10dp in Material Switch using android:padding=”” attribute.
Learn to Set Padding of Material Switch Dynamically
Set Margin of Material Switch
We use android:layout_margin=”” attribute to set margin of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" />
Here, we have set margin of 10dp in Material Switch using android:layout_margin=”” attribute.
Learn to Set Margin of Material Switch Dynamically
Set Background of Material Switch
We use android:background=”” attribute to set background of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />
Here, we have set background of color #ff0000 in Material Switch using android:background=”” attribute.
Learn to Set Background of Material Switch Dynamically
Set Visibility of Material Switch
We use android:visibility=”” attribute to set visibility of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
Here, we have set visibility of Material Switch using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Material Switch Dynamically
Set Text of Material Switch
We use android:text=”” attribute to set text of Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" />
Here, we have set text (“Hello Tutorialwing”) in Material Switch using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of Material Switch Dynamically
Set Color of Text in Material Switch
We use android:textColor=”” attribute to set color of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textColor="#ffffff" />
Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in Material Switch using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of Material Switch Dynamically
Set Gravity of Material Switch
We use android:gravity=”” attribute to set gravity of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:gravity="center_horizontal" />
Here, we have set gravity of text in Material Switch using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of Material Switch Dynamically
Set Text in Uppercase, Lowercase
If we need to show text of Material Switch in uppercase or lowercase etc.
Set text in uppercase
We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textAllCaps="true" />
Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in Material Switch.
By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textAllCaps="false" />
Above code will set Hello Tutorialwing to Material Switch.
How do we set text in lowercase?
Answer –
- In xml file – write all the text in lowercase.
- In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to Material Switch.
Learn to Set Text in Uppercase or Lowercase Dynamically
Set Size of Text in Material Switch
We use android:textSize=”” attribute to set size of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textSize="20sp" />
Here, we have set size of text in Material Switch using android:textSize=”” attribute.
Learn to Set Size of Text of Material Switch Dynamically
Set Style (Bold/italic) of Text in Material Switch
We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:textStyle="bold" />
Here, we have set style of text in Material Switch using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of Material Switch Dynamically
Set Letter Spacing of Text in Material Switch
We use android:letterSpacing=”” attribute to set spacing between letters of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:letterSpacing="1" />
Here, we have set spacing between letters of text in Material Switch using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of Material Switch Dynamically
Set Typeface of Text in Material Switch
We use android:typeface=”” attribute to set typeface in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:typeface="sans" />
Here, we have set typeface of text in Material Switch using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of Material Switch Dynamically
Set fontFamily of Text in Material Switch
We use android:fontFamily=”” attribute to set fontFamily of text in Material Switch.
We can do it as below –
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/materialSwitch_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tutorialwing" android:fontFamily="sans-serif" />
Here, we have set fontFamily (Here, sans-serif) of text in Material Switch using android:fontFamily=”sans-serif” attribute.
Till now, we have see how to use android Material Switch using Kotlin. We have also gone through different attributes of Material Switch to perform certain task. Let’s have a look at list of such attributes and it’s related task.
Different Attributes of Android Material Switch Widget
Below are the various attributes that are used to customise android Material Switch Widget. However, you can check the complete list of attributes of Material Switch in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –
Some of the popular attributes of Switch widget are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:showText | It is used to set whether we want to show text(ON/OFF) or not |
2 | android:textOff | Specifies text to show when Switch is in OFF state. |
3 | android:textOn | Specifies text to show when Switch is in ON state. |
4 | android:textStyle | Sets style (bold, italic etc.) of the text. |
5 | android:thumb | It is used to set drawable to be used as thumb that are moved back and forth. |
6 | android:thumbTextPadding | It sets amount of padding on either side of text within the switch thumb. |
7 | android:thumbTint | It is used to set tint to apply to the thumb |
8 | android:thumbTintMode | It is used to set blending mode used to apply the thumb tint |
9 | android:track | It is used to set drawable of the track in which thumb slides back and forth |
10 | android:typeface | Specifies typeface for the text. For example, normal, sans, serif, monospace etc. |
Some of the popular attributes of android Switch widget inherited from Compound Button are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:button | It is used to set drawable to button |
2 | android:buttonTint | It is used to set tint to apply to button tint |
3 | android:buttonTintMode | It is used to set blending mode used to apply to button graphic tint |
Some of the popular attributes of android Switch inherited from TextView are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:autoLink | It is used to decide whether text such as email, url should be automatically detected and converted into links or not. |
2 | android:cursorVisible | It is used to decide whether cursor should be visible or not. |
3 | android:drawableBottom | It is used to set drawable to be shown below text |
4 | android:ems | It is used to set the view be exactly this ems wide |
Some of the popular attributes of android Switch inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:background | Sets background |
2 | android:clickable | Decides whether view is clickable or not |
3 | android:focusable | Controls whether a view can take focus or not |
4 | android:id | Id of the view |
We have seen different attributes of Material Switch using Kotlin and how to use it. If you wish to visit post to learn more about it
Thus, we have seen what is Material Switch, how can we use android Material Switch using Kotlin ? etc. We also went through different attributes of android Material Switch.
You must be logged in to post a comment.