Öncelikle strings.xml dosyasını oluşturalım.
<resources>
<string name="app_name">Kullanıcı Adı ve Şifre</string>
<string name="test_username">atml</string>
<string name="test_password">1234</string>
<string name="caption">Kullanıcı Girişi</string>
<string name="login_error">Kullanıcı Adı veya Şifre Hatalı</string>
</resources>
Şimdi login.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="@string/caption"
android:textSize="30sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kullanıcı Adı : "
android:textSize="18sp" />
<EditText
android:id="@+id/etUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Şifre : "
android:textSize="18sp" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:ems="10" />
</LinearLayout>
<CheckBox
android:id="@+id/chkRememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="Beni Hatırla" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Giriş" />
</LinearLayout>
Şimdi LoginActivity.java dosyasını oluşturalım.
package com.example.remembermelogin;
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;
import androidx.appcompat.app.AppCompatActivity;
import org.jetbrains.annotations.Nullable;
public class LoginActivity extends AppCompatActivity {
EditText etUserName, etPassword;
CheckBox chkRememberMe;
Button btnLogin;
ShredPreferenc shredPreferenc;
Context context = this;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
init();
if (shredPreferenc.getValueBoolean(context, "remember")) {
etUserName.setText(shredPreferenc.getValue(context, "username"));
chkRememberMe.setChecked(shredPreferenc.getValueBoolean(context, "remember"));
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etUserName.getText().toString().equals(getText(R.string.test_username)) &&
etPassword.getText().toString().equals(getText(R.string.test_password))) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
if (chkRememberMe.isChecked()) {
shredPreferenc.save(context, "username", etUserName.getText().toString());
} else {
shredPreferenc.save(context, "username", "");
}
shredPreferenc.saveBoolean(context, "remember", chkRememberMe.isChecked());
} else {
Toast.makeText(context, getString(R.string.login_error), Toast.LENGTH_SHORT).show();
}
}
});
}
public void init() {
etUserName = findViewById(R.id.etUserName);
etPassword = findViewById(R.id.etPassword);
chkRememberMe = findViewById(R.id.chkRememberMe);
btnLogin = findViewById(R.id.btnLogin);
shredPreferenc = new ShredPreferenc();
}
}
Sıra geldi activity_main.xml dosyasını oluşturmaya.
<?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/txtHosgeldin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Şimdi MainActivity.java dosyasını oluşturalım.
package com.example.remembermelogin;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ShredPreferenc shredPreferenc;
Context context = this;
TextView txtHosgeldin;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtHosgeldin = findViewById(R.id.txtHosgeldin);
shredPreferenc = new ShredPreferenc();
txtHosgeldin.setText(shredPreferenc.getValue(context, "username"));
}
}
Son olarak ShredPreferenc.java sınıfını oluşturalım.
package com.example.remembermelogin;
import android.content.Context;
import android.content.SharedPreferences;
public class ShredPreferenc {
static final String PREF_NAME = "Login";
public void save(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.commit();
}
public void saveBoolean(Context context, String key, Boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
editor.commit();
}
public String getValue(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
String text = settings.getString(key, null);
return text;
}
public Boolean getValueBoolean(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Boolean text = settings.getBoolean(key, false);
return text;
}
public void clear(Context context) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
}
public void remove(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove(key);
editor.commit();
}
}
Şimdi de projeye ait ayarlamaların ve izinlerin yapıldığı manifest dosyasına aşağıdaki satırları ekleyelim.
<activity android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
Projeyi çalıştıralım. Gerekli bilgileri girdiğimizde ikinci sayfanın ekrana geldiğini görürsünüz.