Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

Switch Button in Android Studio | save and get value from Shared Preferences

Switch Button in Android Studio | save and get value from Shared Preferences




In this post, We create a Switch Button in Android Studio and then save it's value to Shared Preferences. Then we create another activity and get that Switch Compat Button value from Shared Preferences.

For that, we create a Switch Compat, and Text View and a button to go to the next activity 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"  >  
   </TextView> 
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:text="Next Activity"
    app:layout_constraintTop_toBottomOf="@id/textview">

</Button>
 </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 btn1;  
   TextView tv;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_my_toggle);  
     switchc = findViewById(R.id.switchbutton);  
     btn1 = findViewById(R.id.button1);  
     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));  
     btn1.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
                startActivity(new Intent(MyToggleActivity.this,NextActivity.class));       
       }  
     });  
   }  
 }  


Then in your Next activity Xml File, create a Text View and a Button to check the state of a Switch Compat.


activitynext.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=".NextActivity">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/textview1"  
     android:text="Checked"  
     android:textSize="16dp"  
     android:padding="20dp">  
   </TextView>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/checkbutton"  
     android:text="check state"  
     app:layout_constraintTop_toBottomOf="@id/textview1">  
   </Button>  
 </android.support.constraint.ConstraintLayout>  


Then in Java File, create a Shared Preferences to get value of Switch Compat and show it on a Text View on a Button Click.


NextActivity.java

 package studio.harpreet.sampleproject;  
 import android.content.SharedPreferences;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.Display;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.TextView;  
 public class NextActivity extends AppCompatActivity {  
   TextView tv;  
   Button btn;  
   SharedPreferences sharedprefs;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_next);  
     tv = findViewById(R.id.textview1);  
     btn = findViewById(R.id.checkbutton);  
     sharedprefs = getSharedPreferences("studio.harpreet.sampleproject", MODE_PRIVATE);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         if(sharedprefs.getBoolean("switch",true))  
         {  
           tv.setText("Checked State");  
         }  
         else  
         {  
           tv.setText("! Checked State");  
         }  
       }  
     });  
   }  
 }  


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

No comments