Android

Beni Hatırla Seçeneğiyle Birlikte Kullanıcı Adı ve Şifre İşlemlerini Gerçekleştiren Android Java Uygulaması

Neredeyse tüm projelerde kullanıcı adı ve şifre işlemleri bulunmaktadır. Kullanıcının sisteme girebilmesi için kullanıcı adı ve şifre bilgilerini girerek uygulamayı kullanmasını sağlayabilirsiniz. Android Studio uygulamalarında da kullanıcı adı ve şifre işlemlerini rahatlıkla gerçekleştirebilirsiniz. Bu makalede beni hatırla seçeneğiyle birlikte kullanıcı adı ve şifre kontrol edildikten sonra uygulamaya başlanmasını sağlayan yapıyı anlatmaya çalışacağız.

Ö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.

 


Etiketler
remember me java mobil 
İlgili Makaleler
App Inventor 2 Uygulamasında Köstebek Oyunu
App Inventor Uygulamasında Belli Bir Süre İçinde Sayı Toplama Oyunu
Android Studio İçin Meb Sertifika Yükleme
Gmail Hesabi Açmak, Gmail Kaydol, Yeni Bir Gmail Hesabı Oluşturma
Personele Ait Yaş Bilgisini Kapsülleyerek Tanımlayan Android Java Uygulaması
Mevsimlere Ait Ayları ViewBinding Yöntemini Kullanarak Android Java Kodlarıyla Tasarlama
Beni Hatırla Seçeneğiyle Birlikte Kullanıcı Adı ve Şifre İşlemlerini Gerçekleştiren Android Java Uygulaması
App Inventor 2 Kurulumu
Android Studio Programında SharedPreferenc Sınıfı Oluşturma
Ksoap Kütüphanesini Android Studio Projesine Dahil Etme
Android Studio Programında Shared Preferences Kullanımı
Android Java Application Using TabbedActivty and Fragment to Login Username and Password
Telefon Araması Yapan Android Java Uygulaması
Girilen Bilgiyi SharedPreferences Kullanarak Kaydedip Görüntüleyen Android Java Uygulaması
Ekran Tema Değişikliği Yapıp Saklayan Android Java Uygulaması
Galeride Bulunan Resimlerden Birini ImageView Nesnesinde Gösteren Android Java Uygulaması
No Activity Seçeneğiyle Oluşturulan Projede Sayıları Arttıran Android Java Uygulaması
Girilen Şehir Adını Görüntüleyen Uygulamayı Android Studio Programında Yapınız
AnyDesk Programıyla Cep Telefonu Görüntüsünü Bilgisayara Aktarma
Yemek Siparişi Veren Form Sayfasını Code Sekmesini Kullanarak Android Studio Programında Yapma

Yorum Ekle
   
Kötü İyi