skip to Main Content

I am working on a project where we want the user to be able to add a 4 digit pin. I want to be able to save it and have the app check upon launch that a pin is set, so that it asks the user for the pin if set or if not set just launch the app. I cannot save this pin in shared preference.

What would be the correct procedure to get this functionality? I will also be adding fingerprint scanner down the line so I need to make sure it works with that.

2

Answers


  1. You can simply do that through Shared Preference also . Get a activity for Set password , make it Default activity . Application will be launch and ask to set password , if user set password then store it through Shared preference and forward them to Main Activity . And check saved password in SetPassword onCreate(),onStart() when it would be created next time .

    On the other hand in Main activity onStart() check the key also .

    Here i am sharing with some simple code –

     <activity android:name=".SetPassword">
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    

    SetPassword Class-

    public class SetPassword extends AppCompatActivity {
    
    EditText passwordview ;
    Button save;
    private SharedPreferences prefs;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);
    
    
         try {
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String password = prefs.getString("password", "");
        if (!password.isEmpty()) {
            sendToMain();
          }
        } catch (Exception e) {
        e.printStackTrace();
        }
    
        passwordview = findViewById(R.id.password);
        save = findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = passwordview.getText().toString();
    
                if (password.isEmpty()) {
                    passwordview.setError("Password required");
                    return;
                }
    
                if (password.contains(" ")) {
                    passwordview.setError("Don't support space");
                    return;
                }
    
                if (password.length() < 4 || password.length() >4) {
                    passwordview.setError("Support 4 digint password");
                    return;
                }
    
                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("password", password);
                editor.commit();
                sendToMain();
    
    
            }
        });
    }
    
        @Override
    protected void onStart() {
        super.onStart();
         SharedPreferences defPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         String password  = defPref.getString("password", "");
         if (!password.isEmpty()) {
            sendToMain();
        }
    }
    
    private void sendToMain() {
        Intent mainIntent = new Intent(SetPassword.this, MainActivity.class);
        startActivity(mainIntent);
        finish();
    }
      }
    

    activity_set_password.xml

        <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout 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=".SetPassword">
    
    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="231dp"
        android:hint="password"
        android:inputType="numberPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="143dp"
        android:layout_marginLeft="143dp"
        android:layout_marginBottom="232dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
      </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity class-

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
              SharedPreferences defPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
              String password  = defPref.getString("password", "");
              if (password.isEmpty()) {
               startActivity(new Intent(MainActivity.this, SetPassword.class));
               finish();
              }
        }
    
    }
    

    If you think about encryption also then you can check AES encryption .

    Login or Signup to reply.
  2. Well you can use SharedPreferences or you can save your data Using normal java InputStream/OutputStream classes

    -in case of shared prefs: you can see the link below
    https://developer.android.com/training/data-storage/shared-preferences

    -in case of java native classes you can access the link below
    https://www.tutorialspoint.com/javaexamples/file_write.htm

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search