• Privacy Policy
  • Contact us
  • Register
  • Log In
  • Facebook
  • Pinterest
  • YouTube
  • WhatsUp
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme

Android Widgets

  • Android DatePicker Example And Tutorial
  • Android SeekBar Exmple and Tutorial
  • Android WebView Example and Tutorial
  • Android RatingBar Tutorial and Example
  • Android ListView Tutorial and Example
  • Android Spinner Tutorial and Examples
  • Android Alert Dialog Tutorial and Example
  • Android Radio Button Tutorials
  • Android CheckBox Tutorial
  • Android Toggle Buttons
  • Android Toast Example
  • Types of Android Button and Examples
  • About Android Studio Widgets

About Android

  • How to Setup Different Android Emulators?
  • What is Android Architecture?
  • History of Android Studio and Version
  • What is Android Studio and Installation
  • Home
  • Courses
  • Android Widgets
  • Android Toggle Buttons

Android Toggle Buttons

Toggle Buttons allows users to change state between two states like as On or Off.

Toggle Buttons can be easily added in your layout xml file using ToggleButton object.

you can ToggleButton like below way in your layout file:

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
Android Toggle Buttons - Toggle Buttons allows users to change state between two states like as On or Off. 
 - Nulled Source Code - NSC - Android Course or Tutorials for Beginners Free
Android Toggle Button

You want to set actions on it then you can do this via java file, but first of all assign a id to it so that we can call it in our java or MainActivity.java file like as: android:id=”@+id/myToggleButton”

<ToggleButton
        android:id="@+id/myToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

Now in your MainActivity.java file initialize the Toogle button via this id and then set the actions on it as shown in the below code inside the onCreate Method:

ToggleButton toggleButton = (ToggleButton)findViewById(R.id.myToggleButton); // initialize the toggle button using id
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    // The toggle is enabled here
                    showToast("Toggle button is enabled"); // showing toast using custom toast method
                } else {
                    // The toggle is disabled here
                    showToast("Toggle button is disabled"); // showing toast using custom toast method
                }
            }
        });

After click on Toggle button it will become on as showing in the screenshot:

Android Toggle Buttons - Nulled Source Code - NSC - Android Course or Tutorials for Beginners Free
Toglle Button is on

If you want to set background and text color on your toggle button than you can add in your layout xml file via this way:

android:background="@color/colorPrimary"
android:textColor="@color/white"

Gradient Toogle Button:

Yes you can also set Gradient color to your Android Toggle button using a drawable file, to set gradient you have to create a gradient drawable file and than set it as background of Toggle Button as like you set background color or text color:

<ToggleButton
        android:id="@+id/myToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/gradeint"
        android:textColor="@color/white"/>
Android Toggle Buttons - Nulled Source Code - NSC - Android Course or Tutorials for Beginners Free
Gradient Toogle off
Android Toggle Buttons - Nulled Source Code - NSC - Android Course or Tutorials for Beginners Free
Gradient Toogle on

This is the gradeint Android Toogle button with both states off and on as shown in the images.

Toggle Button
What are your Feelings
What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on April 24, 2021
Android Toast ExampleAndroid CheckBox Tutorial

Leave a Comment X

You must be logged in to post a comment.

Copyright © 2022.Nuled Source Code.

  • Native Android
  • Paid Projects
  • Flutter
  • My Tickets
  • Register
  • Log In
  • Register
 

Loading Comments...
 

You must be logged in to post a comment.