Breaking News

Switch Button in Android Studio and use of Shared Preferences

Use of Switch Button in Android Studio and Save it's state to Shared Preferences.




In this post, We create a Switch Button in Android Studio and then save it's value to Shared Preferences.

For that, we create a Switch Compat, a Save button, 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>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/button"  
     android:text="save">  
   </Button>  
   <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;  
   Button btn;  
   TextView tv;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_my_toggle);  
     switchc = findViewById(R.id.switchbutton);  
     btn = findViewById(R.id.button);  
     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));  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         if(switchc.isChecked())  
         {  
           editor.putBoolean("switch",true);  
           editor.apply();  
           tv.setText("Checked State");  
         }  
         if(!switchc.isChecked())  
         {  
           editor.putBoolean("switch",false);  
           editor.apply();  
           tv.setText("! Checked State");  
         }  
         editor.commit();  
         Toast.makeText(MyToggleActivity.this, "Setting Saved", Toast.LENGTH_SHORT).show();  
       }  
     });  
   }  
 }  

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