Skip to content
No results

  • Home
  • Portfolio
  • Android Studio
  • Unity
    • Unity 2D
    • Unity 3D
  • About Me
  • Contact
DIGIDIMENSION
  • Home
  • Portfolio
  • Android Studio
  • Unity
    • Unity 2D
    • Unity 3D
  • About Me
  • Contact
SUBSCRIBE
DIGIDIMENSION

Shared Preferences

In this video, we create a simple application login activity in which we use SharedPreferences to save our username and password.

Sample pictures

Java code

SplashScreen
Java
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
package com.example.sharedpreference;
 
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
 
public class SplashScreen extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
 
        StartAnim();
 
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
 
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
 
            }
        }, 3000);
    }
    void StartAnim(){
        TextView ta = findViewById(R.id.splash_text);
        Animation zoomIn;
        zoomIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
        ta.startAnimation(zoomIn);
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
 
loginActivity
Java
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
112
113
114
115
116
117
118
package com.example.sharedpreference;
 
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
 
public class LoginActivity extends AppCompatActivity {
 
 
    public SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    EditText et_name, et_pass;
 
    String name,pass,_name,_pass;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        RemoveToolbar();
        findView();
 
        sharedPreferences = getSharedPreferences("MyShared", MODE_PRIVATE);
        editor = sharedPreferences.edit();
 
    }
    void RemoveToolbar(){
        //Remove toolbar
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    void findView() {
        et_name = findViewById(R.id.actv_log_user_name);
        et_pass = findViewById(R.id.actv_log_password);
    }
 
 
    public void LoginClick(View v) {
 
        name = sharedPreferences.getString("NAME", "");
        pass = sharedPreferences.getString("PASS", "");
        _name = et_name.getText().toString();
        _pass = et_pass.getText().toString();
 
        if(name.equals("") && pass.equals("")){
 
            Toast.makeText(this,"Please register",
                    Toast.LENGTH_SHORT).show();
 
        }
        else if (name .equals(_name) && pass.equals(_pass)) {
 
            Toast.makeText(this,"You have successfully logged in",
                    Toast.LENGTH_LONG).show();
 
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
 
        }
        else {
 
            Toast.makeText(this,"Username or password incorrect",
                    Toast.LENGTH_SHORT).show();
        }
 
    }
 
    public void RegisterClick(View v) {
 
        name = sharedPreferences.getString("NAME", "");
        pass = sharedPreferences.getString("PASS", "");
        _name = et_name.getText().toString();
        _pass = et_pass.getText().toString();
 
        if (!name.equals("") && !pass.equals("")) {
 
            Toast.makeText(this, "You are already registered",
                    Toast.LENGTH_SHORT).show();
 
        }
        else if(!_name.equals("") && !_pass.equals("")){
 
            editor.putString("NAME", _name);
            editor.putString("PASS", _pass);
            editor.apply();
            editor.commit();
 
            Toast.makeText(this, "Save data",
                    Toast.LENGTH_SHORT).show();
 
            et_name.setText(null);
            et_pass.setText(null);
        }
        else {
            Toast.makeText(this, "Username or password fields are empty",
                    Toast.LENGTH_SHORT).show();
        }
 
    }
 
 
    public void ClearClick(View v) {
 
        editor.clear();
        editor.apply();
        editor.commit();
        Toast.makeText(getApplicationContext(), "Clear data",
                Toast.LENGTH_SHORT).show();
    }
}
 

XML

manifest
XHTML
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.sharedpreference">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity"></activity>
        <activity
            android:name=".SplashScreen"
            android:theme="@style/splashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:theme="@style/LogIn" />
    </application>
 
</manifest>
 
activity_login
XHTML
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
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back6"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="100dp"
    tools:context=".LoginActivity">
 
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="150dp"
        app:srcCompat="@drawable/avatar"
        android:contentDescription="@string/todo" />
 
    <EditText
        android:id="@+id/actv_log_user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/un_pass_background"
        android:drawableStart="@drawable/ic_account_box_black_24dp"
        android:drawablePadding="10dp"
        android:hint="@string/user_name"
        android:inputType="text"
        android:padding="10dp"
        android:textColorHint="@color/Text"
        android:autofillHints="" />
 
    <EditText
        android:id="@+id/actv_log_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="10dp"
        android:autofillHints=""
        android:background="@drawable/un_pass_background"
        android:drawableStart="@drawable/ic_vpn_key_black_24dp"
        android:drawablePadding="10dp"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:padding="10dp"
        android:textColorHint="@color/Text" />
 
    <Button
        android:id="@+id/actv_log_login_butt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="30dp"
        android:background="@drawable/login_butt_back"
        android:onClick="LoginClick"
        android:text="@string/login"
        android:textColor="@color/Text"
        android:textSize="20sp" />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:weightSum="2">
 
        <TextView
            android:id="@+id/actv_log_clear_account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="ClearClick"
            android:text="@string/clear_data"
            android:textColor="@color/Text"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/actv_log_create_account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="RegisterClick"
            android:text="@string/register"
            android:textColor="@color/Text"
            android:textSize="18sp" />
 
    </LinearLayout>
 
 
</LinearLayout>
 
activity_splash_screen
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashScreen"
    android:background="@color/SplashScreen"
    android:gravity="center">
 
    <TextView
        android:id="@+id/splash_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shared Preferences"
        android:textColor="@color/Text"
        android:textSize="40sp"
        android:textStyle="bold" />
 
</LinearLayout>
 
zoom_in
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000">
 
    <alpha android:fromAlpha="0"/>
 
    <scale
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1"
    />
 
</set>
 
Previous Post 2D boy animation
Next Post Alien Runner

Leave a ReplyCancel Reply

Your email address will not be published. Required fields are marked *

Recent Works

Popular Projects

Small Waterfall

Reaction speed test

WALLS AND WORDS

Funny AR app

Throw the ball

Categories

  • Android Studio
  • Unity
  • Unity 2D
  • Unity 3D
If you are interested in this and similar content, please subscribe to my YouTube channel.

DIGIDIMENSION

Subscribe Digidimension YouTub channel.

SUBSCRIBE

Site Menu

  • Home
  • Portfolio
  • Android Studio
  • Unity 2D
  • Unity 3D

Information

  • About Me
  • Contact Me

Contact me

Digidimension YouTube

Digidimension Telegram

info@digidimension.ir

Social Icons

Copyright © 2025 - DIGIDIMENSION