Breaking News

Switch Button in Android Studio with on state change listener

Switch Button in Android Studio with on state change listener



In this post, We create a Switch Button in Android Studio and then save it's value to Shared Preferences with the use of on checked change listener.

For that, we create a Switch Compat, and Text View to show Button's State in our XML file.

activitymain.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.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=".MyToggleActivity">  
   <android.support.v7.widget.SwitchCompat  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/switchbutton"  
     android:checked="true">  
   </android.support.v7.widget.SwitchCompat> 
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/textview"  
     android:text="Checked"  
     android:paddingLeft="20dp"  
     android:textSize="16dp"  
     app:layout_constraintTop_toBottomOf="@id/button">  
   </TextView>  
 </android.support.constraint.ConstraintLayout>  

Then we create that Switch compat and shared preferences to save the value of switch compat in our java file.

MainActivity.java
 package studio.harpreet.sampleproject;  
 import android.content.Intent;  
 import android.content.SharedPreferences;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.support.v7.widget.SwitchCompat;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.CompoundButton;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class MyToggleActivity extends AppCompatActivity {  
   SwitchCompat switchc;   
   TextView tv;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_my_toggle);  
     switchc = findViewById(R.id.switchbutton);   
     tv = findViewById(R.id.textview);  
     SharedPreferences sharedPreferences = getSharedPreferences("studio.harpreet.sampleproject",MODE_PRIVATE);  
     final SharedPreferences.Editor editor = sharedPreferences.edit();  
     switchc.setChecked(sharedPreferences.getBoolean("switch",true));  
     switchc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    tv.setText("Checked State");
                    editor.putBoolean("switch",true);
                    editor.apply();
                }
                else
                {
                    tv.setText("! Checked State");
                    editor.putBoolean("switch",false);
                    editor.apply();
                }
                editor.commit();
            }
        });
   }  
 }  

Now, Your Switch Compat Button is successfully created and saved in Shared Preferences.



Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments