Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:45:27+00:00 2026-06-10T08:45:27+00:00

I am hoping someone can help. Still very new to Android/Java. I have an

  • 0

I am hoping someone can help. Still very new to Android/Java. I have an application with multiple activities. When the user click a specific button, I want them to be required to log in before continuing.

I am trying to make it so if the password is NULL it will prompt to create a password and after successfully creating the password it would continue on to the activity. And from that point forward it would ask them for the password they created to get to that activity if the application is restarted.

Here is what I have. I can not seem to get it to SET a password. It always stays NULL. I’m sure I have other issues with this code, but I’m trying to take it one step at a time.

package com.soboapps.todos;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Password extends Activity implements OnClickListener {

public static final String PASSWORD_PREF_KEY ="PasswordSharedPreferences";
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
public static final String KEY_PRIVATE = "KEY_PRIVATE";
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
public static final String PREFS_WRITE = "PREFS_WRITE";
public static final String KEY_WRITE = "KEY_WRITE";
public static final String PREFS_READ_WRITE = "PREFS_READ_WRITE";
public static final String KEY_READ_WRITE = "KEY_READ_WRITE";

EditText pass1, password, passwordEditText;
TextView messages, passHint;
//EditText pass1;
Button btnSubmit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.password);


    PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences settings = this.getSharedPreferences("PasswordSharedPreferences", MODE_PRIVATE);
    String password1 = settings.getString("User Password", "");

    messages = (TextView) findViewById (R.id.text1);
    pass1 = (EditText) findViewById(R.id.txtPassword);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    passHint = (TextView) findViewById(R.id.textView1);

    //passHint.setText(password1);
    passwordEditText = (EditText) findViewById(R.id.password);

    if(password1.isEmpty()) {
        passHint.setText("Set a Secure Password");
    }   
    btnSubmit.setOnClickListener(this);
} 

    public void onClick(View v) {
        // TODO Auto-generated method stub
        //PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences settings = this.getSharedPreferences("PasswordSharedPreferences", MODE_PRIVATE);
        final String password1 = settings.getString("User Password", "");
        final String p1 = pass1.getText().toString();

        if(password1.isEmpty()) {
        //switch(v.getId()){
        //case R.id.btnSubmit:

            if (p1.toLowerCase().equals("password")) {
                pass1.setText("");
                messages.setText("Password cannot be \"password\"");
            }

            if (p1.length() < 3) {
                messages.setText("Password must be at least 3 characters");
            }

            else {

                settings.edit().putString("User Password", p1).apply();
                Toast customToast = new Toast(getBaseContext());
                customToast = Toast.makeText(getBaseContext(), "Password has been set!", Toast.LENGTH_SHORT);
                customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
                customToast.show();
                StartGallery();
            }
        }
            else if(p1.equals(password1)) {

                StartGallery();
                }
                else {
                    Toast customToast = new Toast(getBaseContext());
                    customToast = Toast.makeText(getBaseContext(), "Incorrect Password!", Toast.LENGTH_SHORT);
                    customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
                    customToast.show(); {
                    }
                }
        }

    public void StartGallery(){
            Intent intent = new Intent(this, GalleryActivity.class);
            startActivity(intent);
        }

        public boolean onCreateOptionsMenu(Menu menu){
            super.onCreateOptionsMenu(menu);
            MenuInflater Password = getMenuInflater();
            Password.inflate(R.menu.prefs_menu, menu);
            return true;
        }

        public boolean onOptionsItemSelected(MenuItem item){
            switch(item.getItemId()){
            case R.id.menuPrefs:

            startActivity(new Intent("com.soboapps.todos.PASSPREFS"));
            return true;
        }
            return false;
        }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:45:29+00:00Added an answer on June 10, 2026 at 8:45 am

    I recommend a few changes.

    • Don’t use the SharedPreferences file name as the key for your password, this is confusing.
      You should use a unique string for the password key, this way you can eventually have different password for different users.

    • You overrode the OnClickListener regardless of whether the password succeeded or failed…

    • You were only checking against “password”; not “Password”, “PASSWORD”, “PaSsWoRd”, etc.

    • When the password is set, I think the next Activity should open right away. I don’t think the user should have to click “Submit” twice. I left a suggestion in the code about this.

    Here is a revised OnClickListener:

    public void onClick(View v) {
        SharedPreferences settings = this.getSharedPreferences("PasswordSharedPreferences", MODE_PRIVATE);
        final String password1 = settings.getString("User Password", "");
        final String p1 = pass1.getText().toString();
    
        // No password exists, try to set one
        if(password1.isEmpty()) {
            // Forbid any variation of "password", "Password", "PASSWORD", etc
            if (p1.toLowerCase().equals("password")) {
                pass1.setText("");
                messages.setText("Password cannot be \"password\"");
            }
            // Minimum length requirement
            else if (p1.length() < 3) {
                messages.setText("Password must be at least 3 characters");
            }
            // Set input as password
            else {
                settings.edit().putString("User Password", p1).apply();
                messages.setText("Password set!");
    
                // You could displayed "Password set!" is a Toast and start the next activity immediately
                //StartGallery();
            }
        }
        else if(p1.equals(password1)) {
            StartGallery();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue I'm hoping someone can help with. I have an application
im hoping someone can help me quickly with a question i have. As site
I'm hoping someone can help on this one, I have created a custom object
I am hoping someone can help me with a question i have relating to
I am hoping someone can help me out with a quick question I have
I'm still learning PHP Regex so I'm hoping someone can help me with what
Hoping someone can help me design this correctly. In my TCP code, I have
I am hoping someone can help me. I have just started running xcode 4.01
Hoping someone can help me out with this. Lets say I have a table
I'm hoping someone can help me with a question I have about images and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.