Mevcut cihazda veri-değer mantığıyla veri saklayan shared preferences kullanımı ile ilgili güzel bir örnek yapalım. Örneğimizde kullanıcının girdiği değeri saklamayı, silmeyi, shared preferences yapısını kaldırmayı, saklanan değeri yeni sayfada görüntüleme işlemini gerçekleştireceğiz,
Bu uygulamada neler öğreneceğiz.
İstenen özelliklere göre sayfa tasarımını yapma
Sınıf tanımlama ve kullanma
Girilen değeri sınıf yardımıyla local ortamda kaydetme ve çağırma
Yeni sayfada bilgiyi gösterme
Projede ikinci sayfayı kullanma
Android Studio programını açalım. Boş bir proje oluşturalım. Dil olarak java seçelim. Proje adını SharedPreferencesKullanimi olarak belirleyelim. Proje oluştuktan sonra emulatörü açaılım. Projeyi çalıştıralım.
Herhangi bir hata almadıysak projeyi yazmaya başlayabiliriz. Öncelikle activity_main.xml dosyasını aşağıdaki gibi oluşturalım.
<?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:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginTop="90dp">
<CheckBox
android:id="@+id/chk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<EditText
android:id="@+id/etIsim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaydet" />
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sil" />
<Button
android:id="@+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaldır" />
<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Git" />
</LinearLayout>
Şimdi MainActivity.java dosyasına ait kodları yazalım. Burada uygulamamızda 4 adet buton olduğu için OnClickListener() kütüphanesini implement edip kullanacağız.
package com.example.sharedpreferenceskullanimi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
CheckBox chk1;
EditText etIsim;
Button btnSave, btnDelete, btnRemove, btnGo;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnSave.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnRemove.setOnClickListener(this);
btnGo.setOnClickListener(this);
}
public void init() {
chk1 = findViewById(R.id.chk1);
etIsim = findViewById(R.id.etIsim);
btnSave = findViewById(R.id.btnSave);
btnDelete = findViewById(R.id.btnDelete);
btnRemove = findViewById(R.id.btnRemove);
btnGo = findViewById(R.id.btnGo);
}
@Override
public void onClick(View view) {
}
}
Projeye ikinci bir sayfa ekleyelim. Öncelikle second.xml dosyasını oluşturalım.
<?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:orientation="vertical">
<TextView
android:id="@+id/sonuc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
Bu xml dosyasını kullanacak SecondActivity.java dosyasını oluşturalım ve aşağıdaki kodları düzenleyelim.
package com.example.sharedpreferenceskullanimi;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
text = findViewById(R.id.sonuc);
}
}
İkinci sayfanın projeye dahil olması için aşağıdaki satırı Manifest dosyasına ekleyelim.
<activity android:name=".SecondActivity"></activity>
Projeye ait temel işlemleri gerçekleştirdik. Şimdi kullanıcının girdiği bilgiyi saklamak için sharedpreferences işlemini gerçekleştireceğiz. Bu işlemleri gerçekleştirmek için sınıf tanımlayacağız ve bu sınıfta metodları yazacağız. Konuyla ilgili işlemleri Android Studio Programında SharedPreferenc Sınıfı Oluşturma linkinden öğrenebilirsiniz.
Sıra Geldi tanımladığımız sınıfın projede kullanılması işlemine. Öncelikle MainActivity.java dosyasını aşağıdaki gibi düzenleyelim.
package com.example.sharedpreferenceskullanimi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
CheckBox chk1;
EditText etIsim;
Button btnSave, btnDelete, btnRemove, btnGo;
String text;
SharedPreferenc shredPreferenc;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnSave.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnRemove.setOnClickListener(this);
btnGo.setOnClickListener(this);
}
public void init() {
chk1 = findViewById(R.id.chk1);
etIsim = findViewById(R.id.etIsim);
btnSave = findViewById(R.id.btnSave);
btnDelete = findViewById(R.id.btnDelete);
btnRemove = findViewById(R.id.btnRemove);
btnGo = findViewById(R.id.btnGo);
shredPreferenc = new SharedPreferenc();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
if (chk1.isChecked()) {
text = etIsim.getText().toString();
shredPreferenc.save(context, text);
Toast.makeText(context, "Kaydedildi",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnDelete:
shredPreferenc.clear(context);
Toast.makeText(context, "Silindi",
Toast.LENGTH_SHORT).show();
break;
case R.id.btnRemove:
shredPreferenc.remove(context);
Toast.makeText(context, "Kaldırıldı",
Toast.LENGTH_SHORT).show();
break;
case R.id.btnGo:
Intent intent = new Intent(context, SecondActivity.class);
startActivity(intent);
break;
}
}
}
Daha sonra SecondActivity.java dosyasını düzenleyelim.
package com.example.sharedpreferenceskullanimi;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
TextView text;
SharedPreferenc shredPreferenc;
Context context = this;
String donenDeger;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
text = (TextView) findViewById(R.id.sonuc);
shredPreferenc = new SharedPreferenc();
donenDeger = shredPreferenc.getValue(context);
text.setText(donenDeger);
}
}
Projeyi kaydedip çalıştıralım. Metin kutusuna "sifre" yazalım. Onay kutusunu işaretleyelim. Kaydet butonuna basalım. Projeden çıkıp tekrar çalıştıralım. Git butonuna basalım. Girdiğimiz değer alan "sifre" bilgisi ekranda görüntülenecektir.