Android Toggle Button Using Kotlin With Example

In this article, we will learn about android Toggle Button using Kotlin. We will go through various example that demonstrates how to use different attributes of ToggleButton. For example, …..

In this article, we will get answer to questions like –

  • What is ToggleButton?
  • Why should we consider Toggle Button while designing ui for any app?
  • What are possibilities using Toggle Button while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Toggle Button Output Android Toggle Button Widget Tutorial

Tutorialwing Toggle Button Output

Getting Started

We can define android Toggle Button widget as below –

Android toggle button is a widget that are used to display two states(checked/unchecked or ON/OFF) as a button with light indicator and default text ON or OFF.

Now, how do we use Toggle Button 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 ToggleButton. 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 Toggle Button 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 Toggle Button in Kotlin

Follow steps below to use Toggle Button in newly created project –

  • Open res/layout/activity_main.xml file. Then, add code for toggle button 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">
    
        <ToggleButton
            android:id="@+id/toggleButton_ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Here, we have defined toggle button in xml file. Now, we will access this toggle button using kotlin file. Then, we will show toast message when state of toggle button is changed.

  • We can also access toggleButton in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.togglebutton
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import com.tutorialwing.togglebutton.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)
    
    		setCheckedChangedListener()
    	}
    
    	private fun setCheckedChangedListener() {
    		binding.toggleButtonID.setOnCheckedChangeListener { buttonView, isChecked ->
    			val msg = "Toggle Button is " + if (isChecked) "ON" else "OFF"
    			Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
    		}
    	}
    }
    

    Here, we have accessed toggleButton as binding.toggleButtonID in Kotlin file. Then, we have set a listener which will be called whenever checked state of toggle button is changed.

Now, run the application. We will get output as below –

Tutorialwing Toggle Button Output Android Toggle Button Widget Tutorial

Tutorialwing Toggle Button Output

Different Attributes of Toggle Button in XML

Now, we will see how to use different attributes of Android Toggle Button using Kotlin to customise it –

Set Id of ToggleButton

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 Toggle Button using android:id attribute like below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        />

Here, we have set id of Toggle Button as toggleButton_ID using android:id=”” attribute. So, if we need to reference this ToggleButton, we need to use this id – toggleButton_ID.
Learn to Set ID of Toggle Button Dynamically

Set Width of ToggleButton

We use android:layout_width=”” attribute to set width of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button Dynamically

Set Height of ToggleButton

We use android:layout_height=”” attribute to set height of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button Dynamically

Set Padding of ToggleButton

We use android:padding=”” attribute to set padding of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in Toggle Button using android:padding=”” attribute.
Learn to Set Padding of Toggle Button Dynamically

Set Margin of ToggleButton

We use android:layout_margin=”” attribute to set margin of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in Toggle Button using android:layout_margin=”” attribute.
Learn to Set Margin of Toggle Button Dynamically

Set Background of ToggleButton

We use android:background=”” attribute to set background of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in Toggle Button using android:background=”” attribute.
Learn to Set Background of Toggle Button Dynamically

Set Visibility of ToggleButton

We use android:visibility=”” attribute to set visibility of ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of Toggle Button using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Toggle Button Dynamically

Set Text of ToggleButton

We use android:textOn=”” and android:textOff=”” attribute to set text of ToggleButton when it is ON or OFF state respectively.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Hi Tutorialwing"
        android:textOff="Bye Tutorialwing" />

Here, we have set text (“Hi Tutorialwing”) in Toggle Button using android:textOn=”” attribute when toggle button is in ON state. Similarly, we have set text (“Bye Tutorialwing”) in Toggle Button using android:textOff=”” attribute when toggle button is in OFF state.
Similarly, we can set any text using these attributes.
Learn to Set Text of Toggle Button Dynamically

Set Color of Text in ToggleButton

We use android:textColor=”” attribute to set color of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of Toggle Button Dynamically

Set Gravity of ToggleButton

We use android:gravity=”” attribute to set gravity of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of Toggle Button Dynamically

Set Text in Uppercase, Lowercase

If we need to show text of Toggle Button 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 –

    <ToggleButton
        android:id="@+id/toggleButton_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 ToggleButton.

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,

    <ToggleButton
        android:id="@+id/toggleButton_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 ToggleButton.

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 ToggleButton.

Learn to Set Text in Uppercase or Lowercase Dynamically

Set Size of Text in Toggle Button

We use android:textSize=”” attribute to set size of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:textSize=”” attribute.
Learn to Set Size of Text of Toggle Button Dynamically

Set Style (Bold/italic) of Text in Toggle Button

We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of Toggle Button Dynamically

Set Letter Spacing of Text in Toggle Button

We use android:letterSpacing=”” attribute to set spacing between letters of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of Toggle Button Dynamically

Set Typeface of Text in Toggle Button

We use android:typeface=”” attribute to set typeface in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of Toggle Button Dynamically

Set fontFamily of Text in Toggle Button

We use android:fontFamily=”” attribute to set fontFamily of text in ToggleButton.
We can do it as below –

    <ToggleButton
        android:id="@+id/toggleButton_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 Toggle Button using android:fontFamily=”sans-serif” attribute.

Till now, we have see how to use android Toggle Button using Kotlin. We have also gone through different attributes of Toggle Button to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android Toggle Button Widget

Below are the various attributes that are used to customise android Toggle Button Widget. However, you can check the complete list of attributes of Toggle Button in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Different attributes of toggle button are –

Sr. XML Attributes Description
1 android:disabledAlpha Value of alpha to set when indicator is disabled.
2 android:textOff Text to show when toggle button is OFF.
3 android:textOn Text to show when toggle button is ON.

Attributes in Toggle Button are also inherited from TextView and Compound Button. Some of the popular attributes inherited from TextView are –

Sr. XML Attributes Description
1 android:background Sets background to toggle button.
2 android:backgroundTint Used to set tint to background.
3 android:clickable Sets true when you want to make toggle button clickable. Otherwise, set false.
4 android:drawableBottom Drawable to draw at bottom of the text.
5 android:drawableEnd Drawable to draw at end of the text.
6 android:drawableLeft Drawable to draw at left of the text.
7 android:drawablePadding Padding of the drawable.
8 android:drawableRight Drawable to draw at right of the text.
9 android:drawableStart Drawable to draw at start of the text.
10 android:drawableTop Drawable to draw at top of the text.
11 android:text Sets text of the Toggle Button.
12 android:textAllCaps Shows text in capital letters.
13 android:textColor Sets color of the text.
14 android:textSize Sets text size. For example, 12sp, 13sp etc.
15 android:textStyle Sets text style. For example, bold, italic, bolditalic etc.
16 android:typeface Sets typeface of the text. For example, normal, sans, monospace etc.

Attributes of Toggle Button inherited from Compound Button are –

Sr. XML Attributes Description
1 android:button Drawable for button graphic (for example, checkbox and radio button).
2 android:buttonTint Tint to button graphic.

We have seen different attributes of Toggle Button and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ToggleButton, how can we use android Toggle Button using Kotlin ? etc. We also went through different attributes of android ToggleButton.

Leave a Reply