Change password for Parse.com Android (Front-end check) - java

I'm trying to produce a password check before submitting my form, but I'm not getting the desired behavior. Basically no matter what the user inputs for either field, it will submit the new password from mConfirmPasswordField. What I want to have happen is that if the passwords do not match, hence if (!(mNewPassword.equals(mConfirmPassword)), then an alert dialog is displayed and nothing more. Based on the code below, this seems like it should be the case but it simply saves the new password to the user either way. What am I doing wrong here?
mNewPasswordField = (EditText)findViewById(R.id.newPassword);
mConfirmPasswordField = (EditText)findViewById(R.id.newPasswordAgain);
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
mNewPasswordField.setText(mNewPassword);
mConfirmPasswordField.setText(mConfirmPassword);
Button mButton = (Button) findViewById(R.id.submitPasswordChanges);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!(mNewPassword.equals(mConfirmPassword))) {
AlertDialog.Builder builder = new AlertDialog.Builder(ChangePasswordActivity.this);
builder.setMessage("Please check that you've entered and confirmed your new password!")
.setTitle("Error:")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
setProgressBarIndeterminateVisibility(true);
//Update user
ParseUser user = ParseUser.getCurrentUser();
user.setPassword(mConfirmPasswordField.getText().toString());
user.saveInBackground(new SaveCallback() {
public void done(com.parse.ParseException e) {
// TODO Auto-generated method stub
LaunchPersonalGalleryIntent();
}
});
}
}

final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
This will be executed when the EditText are still empty, so mNewPassword equals mConfirmPassword equals "".
The two String should be retrieved within the onClick:
..
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
if (!(mNewPassword.equals(mConfirmPassword))) {
..

I believe what's wrong is that you are not updating your fields: you should check for new values right when the user clicks the button, like:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mNewPassword = mNewPasswordField.getText().toString();
mConfirmPassword = mConfirmPasswordField.getText().toString();
if (!(mNewPassword.equals(mConfirmPassword))) {
....
} else {
....
}
}
});

I should mention for others passing through, that you will also need to include the following fields, along with the password strings, within the onClick method.
..
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mNewPasswordField = (EditText)findViewById(R.id.newPassword);
mConfirmPasswordField = (EditText)findViewById(R.id.newPasswordAgain);
final String mNewPassword = mNewPasswordField.getText().toString();
final String mConfirmPassword = mConfirmPasswordField.getText().toString();
mConfirmPasswordField.setText(mConfirmPassword);
if (!(mNewPassword.equals(mConfirmPassword))) {
..

Related

IF statement skipping to else when it should be true [duplicate]

I am trying to create a basic create account system (its my first day using android studio so dont judge too harshly) everytime i test the program it simply defaults straight to else even when the two passwords are vastly different. Thank you all very much for your help in advance.
public class CreateAccount extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
Button Submit_btn = (Button) findViewById(R.id.Submit_btn);
final EditText Username_txt = (EditText)findViewById(R.id.Username_txt);
final EditText Email_txt = (EditText)findViewById(R.id.Email_txt);
final EditText Password_txt = (EditText)findViewById(R.id.Password_txt);
final EditText VerifyPassword_txt = (EditText)findViewById(R.id.VerifyPassword_txt);
final TextView Error_txt = (TextView)findViewById(R.id.Error_txt);
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
}
You should add trim()
Returns a string whose value is this string, with any leading and
trailing whitespace removed.
Submit_btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
final String Password_string = Password_txt.getText().toString().trim();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString().trim();
if (!Password_string.equals(VerifyPassword_string))
{
Error_txt.setText("Passwords must be matching");
} else
{
Error_txt.setText("No Error");
}
}
});
change your onClick method to retrieve the values
public void onClick(View view) {
Password_string = Password_txt.getText().toString();
VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
note
Also try to use java standard naming conventions
e.g.
String passwordString = "";
You hardcode the retrieved values from input widgets during the initialization of the Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string =
VerifyPassword_txt.getText().toString();
...
}
You have the issue for password inputs, but you would have the same problem for email and user inputs.
You should get all of them dynamically.
For example for password inputs:
public void onClick(View view) {
if (!Password_txt.getText().toString().equals
(VerifyPassword_txt.getText().toString())) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
You are trying to get the values of your EditText at the time of onCreate it self at which time they won't have any values in them as you might be typing them only after screen is open. So you should do this calculation only on your onclick -
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
So understand the activity Lifecycle here. onCreate is called to setup your View as the first method to your Activity. At that time your view doesn't have any user filled data in it. Once the view is populated and you see the UI, then the user interacts with it. So any computation that you need to do after user input, has to be action based(in this case your btn click) and hence try to get the data always at the time of action.
try this
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
I have made some changes try it now
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
//final String Password_string = Password_txt.getText().toString();
//final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String passwordStr= Password_txt.getText().toString();
String passwordConfirmStr=VerifyPassword_txt.getText().toString();
if (!passwordStr.equals(passwordConfirmStr)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
});

What am i doing wrong at my simple android app?

I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.

Android Studio .compareTo and .equals with strings not working

I am trying to create a basic create account system (its my first day using android studio so dont judge too harshly) everytime i test the program it simply defaults straight to else even when the two passwords are vastly different. Thank you all very much for your help in advance.
public class CreateAccount extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
Button Submit_btn = (Button) findViewById(R.id.Submit_btn);
final EditText Username_txt = (EditText)findViewById(R.id.Username_txt);
final EditText Email_txt = (EditText)findViewById(R.id.Email_txt);
final EditText Password_txt = (EditText)findViewById(R.id.Password_txt);
final EditText VerifyPassword_txt = (EditText)findViewById(R.id.VerifyPassword_txt);
final TextView Error_txt = (TextView)findViewById(R.id.Error_txt);
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
}
You should add trim()
Returns a string whose value is this string, with any leading and
trailing whitespace removed.
Submit_btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
final String Password_string = Password_txt.getText().toString().trim();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString().trim();
if (!Password_string.equals(VerifyPassword_string))
{
Error_txt.setText("Passwords must be matching");
} else
{
Error_txt.setText("No Error");
}
}
});
change your onClick method to retrieve the values
public void onClick(View view) {
Password_string = Password_txt.getText().toString();
VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
note
Also try to use java standard naming conventions
e.g.
String passwordString = "";
You hardcode the retrieved values from input widgets during the initialization of the Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string =
VerifyPassword_txt.getText().toString();
...
}
You have the issue for password inputs, but you would have the same problem for email and user inputs.
You should get all of them dynamically.
For example for password inputs:
public void onClick(View view) {
if (!Password_txt.getText().toString().equals
(VerifyPassword_txt.getText().toString())) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
You are trying to get the values of your EditText at the time of onCreate it self at which time they won't have any values in them as you might be typing them only after screen is open. So you should do this calculation only on your onclick -
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
So understand the activity Lifecycle here. onCreate is called to setup your View as the first method to your Activity. At that time your view doesn't have any user filled data in it. Once the view is populated and you see the UI, then the user interacts with it. So any computation that you need to do after user input, has to be action based(in this case your btn click) and hence try to get the data always at the time of action.
try this
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String Password_string = Password_txt.getText().toString();
final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
if (!Password_string.equals(VerifyPassword_string)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
I have made some changes try it now
final String Username_string = Username_txt.getText().toString();
final String Email_string = Email_txt.getText().toString();
//final String Password_string = Password_txt.getText().toString();
//final String VerifyPassword_string = VerifyPassword_txt.getText().toString();
Submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String passwordStr= Password_txt.getText().toString();
String passwordConfirmStr=VerifyPassword_txt.getText().toString();
if (!passwordStr.equals(passwordConfirmStr)) {
Error_txt.setText("Passwords must be matching");
} else {
Error_txt.setText("No Error");
}
}
});

Checking if a username and email are found in Parse database

In my application I have an activity called AddPatient this activity allows the user to enter either a username or a email and the if found the text view will display a text "Found" else "not found" and the button will allow the user to continue to the next activity just if the users found the mail and username already in database. This is my code but it's giving me found all the time and the button is not working....Any help please.
Note this activity extends AppCompatActivity:
EditText UserNameEt;
EditText EmailEt;
String email;
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_patient);
UserNameEt = (EditText) findViewById(R.id.et1);
EmailEt = (EditText) findViewById(R.id.et2);
username = UserNameEt.getText().toString();
email = EmailEt.getText().toString();
final TextView tv = (TextView) findViewById(R.id.tv);
ParseQuery<ParseObject> lotsOfWins = ParseQuery.getQuery("User");
lotsOfWins.whereEqualTo("email",email);
ParseQuery<ParseObject> fewWins = ParseQuery.getQuery("User");
fewWins.whereEqualTo("username", username);
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(lotsOfWins);
queries.add(fewWins);
ParseQuery<ParseObject> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (true) {
tv.setText("Patient found");
} else {
tv.setText("Patient Not found");
}
}
});
String Result = tv.getText().toString();
if (Result=="Patient found"){
Button fill = (Button) findViewById(R.id.button);
fill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(AddPatient.this,Patients.class);
startActivity(i);
}
});
}
}
}
You're setting whether the patient has been found or not using if (true), which is obviously always going to be true. You need to replace that with something that checks whether the entered value actually exists.
i would like to try on this way.
first set this method.
1st Method.
ParseQuery<ParseObject> lotsOfWins =new ParseQuery<ParseObject>("User");
lotsOfWins.whereEqualTo("email", email);
ParseQuery<ParseObject> fewWins = new ParseQuery<ParseObject>("User");
fewWins.whereEqualTo("username", username);
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(lotsOfWins);
queries.add(fewWins);
ParseQuery<ParseObject> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
tv.setText("Patient found");
} else {
tv.setText("Patient Not found");
}
getPatientInfo(tv.getText().toString());
} else {
// error
}
}
});
2nd this method.
protected void getPatientInfo(String Result) {
// TODO Auto-generated method stub
if (Result == "Patient found") {
Button fill = (Button) findViewById(R.id.button);
fill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(AddPatient.this, Patients.class);
startActivity(i);
}
});
}
}
this type do for findInBackGround is one type of thread.

How to implement onClick in my activity

I have list of passwords (strings) in a database.
In my activity i put EditText (the user will write there his/her password) and a button.
How i can implement onClick for the button that will check if the password are in the system (in the list)?
Define a method called onClick() in your activity - this will get called when the button is clicked (as you specified in your XML). You can then retrieve the EditText text, and check it against the passwords in your DB.
public void onClick(View v) {
EditText myEditText = (EditText) findViewById(R.id.password);
CharSequence enteredPassword = myEditText.getText();
// TODO check if input matches a string in the DB
}
Implement OnClickListener to the control you want the user to click on:
Button mLoginButton = (Button) findViewById(R.id.login);
mLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Login();
}
});
Create the listener event "Login":
private void attemptLogin() {
//check login is valid
}
Add this to the button
btn.addActionListener( this );
and then do this
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == btn )
{
}
}
You can do like this:
Button loginButton = (Button) findViewById(R.id.login);
loginButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String pwd = password.getText().toString();
checkLogin(pwd)
}
})
private void checkLogin(String password) {
//db is your db instance.this is dummy query.You may have a user name editText as well
Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE TRIM(password) = '"+password.trim()+"'", null);
//check if cursor returns data
if (!(c .moveToFirst()) || c .getCount() ==0){
//cursor is empty
}
else
{
`//cursor is not empty`
}
}

Categories

Resources