Bu dersimizde shared preference dosyamızla uğraşırken kullanacağımız remove ve clear komutlarından bahsediyoruz. Ayrıca switch butonuna göre arka plan renginin değiştirilmesi ve bu ayarın saklanmasında sharedpreference dosyamızı nasıl kullanacağımızı anlatıyoruz. Bu dosya sayesinde kullanıcı uygulamayı kapatıp tekrar açtığında yaptığı ayarla uygulamayı kullanabilecek.
clear() komutu : Bu komut belirttiğimiz sharedpreference dosyasının tüm içeriğini temizler. Bu komutu kullanmak için editor nesnemizi oluşturmamız gerekir. editor.clear() dememiz yeterlidir.
remove() komutu : Bu komuta verdiğimiz anahtar değerin karşılığındaki veriyi ve anahtarı siler. Bu komutu kullanmak için editör nesnemizi oluşturmamız gerekir. editor.remove(“key_deger”) deyip istediğimiz kaydı silebiliriz.
Videoda ayrıca switch butonunun nasıl kullanılacağını ve de bunun değerini dosyada saklayarak kullanıcının ayarlarını nasıl tutacağımızı bulabilirsiniz.
Bu dersimizde içeriği değiştirilen MainAtivity ve SecondActivity sınıflarının son halini aşağıda bulabilirsiniz. SharedPreferenceDemo projesinin bitmiş son halini buradaki linkten indirebilirsiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.SwitchCompat; import android.view.View; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText etIsim, etMeslek; private TextView tvIsim, tvMeslek, tvID; private Switch switchCompat; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { sayfaArkaPlaniDegistir(isChecked); } }); //Dosyaya yazılan ayarları getirmek için SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".myFile.xml", MODE_PRIVATE); boolean isChecked = sharedPreferences.getBoolean(Sabitler.KEY_RENK, false); switchCompat.setChecked(isChecked); } private void sayfaArkaPlaniDegistir(boolean isChecked) { //switch butonunun durumunu dosyaya kaydetmek için SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".myFile.xml", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(Sabitler.KEY_RENK, isChecked); editor.apply(); /* if(isChecked==true){ layout.setBackgroundColor(Color.GREEN); }else layout.setBackgroundColor(Color.WHITE); */ layout.setBackgroundColor(isChecked ? Color.GREEN : Color.WHITE); } public void init() { etIsim = (EditText) findViewById(R.id.etName); etMeslek = (EditText) findViewById(R.id.etProfession); tvIsim = (TextView) findViewById(R.id.txvName); tvMeslek = (TextView) findViewById(R.id.txvProfession); tvID = (TextView) findViewById(R.id.txvID); switchCompat = (Switch) findViewById(R.id.pageColorSwitch); layout = (LinearLayout) findViewById(R.id.pageLayout); } public void saveAccountData(View view) { SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".myFile.xml", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(Sabitler.KEY_ISIM, etIsim.getText().toString()); editor.putString(Sabitler.KEY_MESLEK, etMeslek.getText().toString()); editor.putInt(Sabitler.KEY_ID, 150); editor.apply(); Toast.makeText(this, "Veriler Kaydedildi", Toast.LENGTH_SHORT).show(); //editor.commit(); } public void loadAccountData(View view) { SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".myFile.xml", MODE_PRIVATE); String isim = sharedPreferences.getString(Sabitler.KEY_ISIM, "N/A"); String meslek = sharedPreferences.getString(Sabitler.KEY_MESLEK, "N/A"); int id = sharedPreferences.getInt(Sabitler.KEY_ID, 0); tvIsim.setText(isim); tvMeslek.setText(meslek); tvID.setText("" + id); } public void newActivity(View view) { Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); } } |
1 2 3 4 5 6 7 |
public class Sabitler { public static final String KEY_ISIM= "isim"; public static final String KEY_MESLEK= "meslek"; public static final String KEY_ID= "id"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/pageLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" tools:context="com.example.emrealtunbilek.sharedpreferencesornek.MainActivity"> <Switch android:id="@+id/pageColorSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Yeşile Çevir "/> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="74dp" android:hint="Adınızı giriniz"/> <EditText android:id="@+id/etProfession" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Mesleğinizi giriniz"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:onClick="saveAccountData" android:text="KAYDET"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="loadAccountData" android:text="GETİR"/> </LinearLayout> <TextView android:id="@+id/txvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="Kişi Adı:" android:textColor="#969494" android:textSize="25sp" android:textStyle="bold"/> <TextView android:id="@+id/txvProfession" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Meslek:" android:textColor="#969494" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { TextView tvIsim, tvMeslek; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); tvIsim= (TextView) findViewById(R.id.txvName); tvMeslek= (TextView) findViewById(R.id.txvProfession); } public void loadAccountData(View view) { SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+".myFile.xml", MODE_PRIVATE); String isim=sharedPreferences.getString(Sabitler.KEY_ISIM,"N/A"); String meslek=sharedPreferences.getString(Sabitler.KEY_MESLEK, "N/A"); int id=sharedPreferences.getInt(Sabitler.KEY_ID, 0); tvIsim.setText(isim); tvMeslek.setText(meslek + " - " + ""+id); } public void clearAccountData(View view) { SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+".myFile.xml", MODE_PRIVATE); SharedPreferences.Editor editor= sharedPreferences.edit(); editor.clear(); editor.apply(); //editor.commit() } public void removeProfessionKey(View view) { SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+".myFile.xml", MODE_PRIVATE); SharedPreferences.Editor editor= sharedPreferences.edit(); editor.remove(Sabitler.KEY_ID); editor.apply(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="VERI GETIR" android:layout_gravity="center" android:onClick="loadAccountData" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="TEMIZLE" android:layout_gravity="center" android:onClick="clearAccountData" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="SİL" android:layout_gravity="center" android:onClick="removeProfessionKey" android:layout_weight="1"/> </LinearLayout> <TextView android:id="@+id/txvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="Isim :" android:textColor="#969494" android:textSize="25sp" android:textStyle="bold"/> <TextView android:id="@+id/txvProfession" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Meslek :" android:textColor="#969494" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout> |