I created a login application. But when I input a wrong password, the app won't give me any notification that the password was incorrect and it's because I didn't even put any codes for it. I don't know what to put in order to achieve it. I'm a newbie here. Please help.
This is my code:
public void onClick(View v)
{
EditText passwordEditText = (EditText) findViewById(R.id.currentPass);
SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
String password = prefs.getString("password","");
if("".equals(password))
{
Editor edit = prefs.edit();
edit.putString("password",passwordEditText.getText().toString());
edit.commit();
StartMain();
}
else
{
if(passwordEditText.getText().toString().equals(password))
{
StartMain();
}
}
}
You probably want an else condition on your inner if statement:
if(passwordEditText.getText().toString().equals(password)) //checking if they put the right password?
{
StartMain(); //I assume this is starting the application
}
else
{
//Tell them the password was wrong.
}
The simplest think would be to show a Toast notification. You could do that in the else branch of the second (nested) if in your code above.
Hello schadi:
i think you problem is this:EditText catch the click event as first as you click it that you should want to input some password.so,your code should always running at '"".equals(password)' statement.
you may can use TextWatcher or something else to do your work.
Chinese guy,English is poor,i hope you can understand what i am saying :)
I suggest using a toast to notify the user. Like this:
else
{
if(passwordEditText.getText().toString().equals(password))
{
StartMain();
}
} else {
Toast.makeText(v.getContext(), "Wrong password!", Toast.LENGTH_LONG).show();
}
Related
I am using the post comment system in android, I want to create a chat option upon clicking the button only among the post author and comment author only, if anyone else clicks it should not open the chat room.
I am using the below code for the same purpose, but it is not working the way required. you can refer the idea of chat system in stackoverflow, the similar thing I want. the below code is in adapter class linking to the button on the recyclerview item. I am the PostAuthorId and CommentAuthorId are already confirmed, I have to just set the condition if the login user is the same as one of them.
i am using firestore database for the same
if any more information is required do let me know.
commentsViewHolder.chat.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
if(PostAuthorID == mAuth.getUid() || CommentAuthorId == mAuth.getUid())
{
Toast.makeText(CommentActivity.this, "Hello", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CommentActivity.this, ChatRoomActivity.class);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_ID, ChatRoomId);
intent.putExtra(ChatRoomActivity.CHAT_ROOM_NAME, ChatRoomName);
startActivity(intent);
}
else
{
Toast.makeText(CommentActivity.this, "You are not authenticated", Toast.LENGTH_SHORT).show();
}
}
});
Probably the problem is that == won't work for strings.
Use PostAuthorID.equals(mAuth.getUid()) to compare strings, not the == operator. mAuth.getUid() returns a String and I am assuming that PostAuthorID is a String too
Like:
if(PostAuthorID.equals(mAuth.getUid()) || CommentAuthorId.equals(mAuth.getUid())){
....
} else {
....
}
Remember:
== operator only compares object references, while the String.equals() method compares both String's values!
How to implement more into this code so that it returns a message telling the user to 'select at least one checkbox' if he/ she does not select any?
public void verificaCheckBox() {
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
}
public String verificaCheckBox(){
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
return Listcheck.isEmpty() ? "Message goes here" : "";
}
Then wherever calls it can check to see if the return is an empty string or the message. If you just want to show the user a message you could do this:
public void verificaCheckBox(){
Listcheck.clear();
if (cbPapel.isChecked())
Listcheck.add(cbPapel.getText().toString());
if (cbPlastico.isChecked())
Listcheck.add(cbPlastico.getText().toString());
if (cbMetal.isChecked())
Listcheck.add(cbMetal.getText().toString());
if (cbVidro.isChecked())
Listcheck.add(cbVidro.getText().toString());
cbSelecionado = (Listcheck.toString());
if(Listcheck.isEmpty()) {
Toast.makeText(applicationContext, "Your message", Toast.LENGTH_SHORT).show();
}
}
Try using an Alert Dialog to accomplish that.
I believe that the following link gives more relevant details on how to do so:
How to add message box with ok button
To put it for you in simpler steps:
Add an else-statement at the end of your code.
Use the Alert Dialog to notify the user of the changes needed to be done.
I am not sure that I have understood your question, but if you want to send message to the user you can use a Toast
For example:
if (Listcheck.size() == 0)
Toast.makeText(this, "select at least one checkbox", Toast.LENGTH_SHORT).show();
I'm trying to login using Parse for android.
If I enter the correct username and password, I log in successfully.
But when I use a wrong password or username, I always get error 101: object not found.
Here's the code (Notice "username" and "password" are EditText):
private void doLogin() {
if (!validate()) { // IF VALIDATION FAILS, DO NOTHING
return;
} // ELSE...
String name = email.getText().toString();
String pass = password.getText().toString();
ParseUser.logInInBackground(name, pass, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
goToMainActivity(user.getUsername());
} else {
handleParseError(e);
}
}
});
}
Thanks for your help.
Update: Parse does not have means to check if there was an incorrect login field. Hence they use the general 101: Object Not Found error to catch it. Reference: https://parse.com/docs/android/api/com/parse/ParseException.html
Previous stackoverflow link: Parse : invalid username, password
If you want your app to respond to an incorrect login, just replace the line handleParseError(e); with code to handle it.
For example, if you want a message box to show up, place that code there. If you do not want to do anything, comment out that line. Not sure what else you are looking for...
I would suggest replacing it with a Toast message
I know that System.out.println() does not work on Android.
So I need another way to print out some text.
Please help me.
I'm using the Root Tools library
class superuser {
public static Command c ;{
if (RootTools.isRootAvailable()) {
System.out.print("Root found!!");
}
else{ System.out.print(("NO ROOT!"));
}
}
}
Outputing text in android
There are many ways, but usually for testing and debugging processes we use log. The log is not visible to user but you can see it in DDMS.
From what i understand you want to create a dialog or display a textview to users so they know if root is available or not.
1.Logging(for testing and debugging processes)
we defined TAG in below code because it would be easy to make changes later and our code is more organised
private static final String TAG = MyActivity.class.getName();
Log.v(TAG , "here is the line i want to output in logcat");
here v in log.v stands for verbose.You may use i for info, e for error etc.
2.Displaying text to user via a TextView
First lets import the textview. Let the id of the textview you imported may be "resultTextView"
TextView resultText = (TextView) findViewById(R.id.resultTextView);
now applying your logic and setting its text...
if (RootTools.isRootAvailable()) {
resultText.setText("Root found!!");
}
else{ resultText.setText(("NO ROOT!"));
}
3.Creating a dialog
Dialogs are the pop out messages we get.
I would recommend creating a function that takes String message and a String Title as a parameter and creates a dialog using dialog.builder something like this rather than a dialog fragment(which is available in the below link) - http://developer.android.com/reference/android/app/AlertDialog.Builder.html
public void alertDialog(String message,String title){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title
alertDialogBuilder.setTitle(title);
// set dialog message
alertDialogBuilder
.setMessage(message)
.setPositiveButton("OK", null) //we write null cause we don't want
//to perform any action after ok is clicked, we just want the message to disappear
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
Now you can call the method with title and text you want :)
if(condition){
Log.d("message","The root found");
}
else{
Log.d("message","The root not found");
}
Have you tried using logcat ? http://developer.android.com/reference/android/util/Log.html
use it like so: Log.v("myAwesomeApp", "my developer comment");
then use the log panel in your IDE to read it
Using logcat is the usual way in Android, and the simplest.
Log.d("message_id","message content");
If you want another way to display log, you can try
https://github.com/orhanobut/logger
I'm really stuck here and it simply doesn't make any sense to me. Here's what's going on: I have an AsyncTask sending data to a server and retrieving a String response back. The String response can be two things: it can be a message telling the user that he/she is logged in if the credentials entered are correct or it can be a "FALSE" string which I use to display a notification toast to the user and take him/her back to the main activity to retry logging in.
Here is the code snippet:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String cr = "Placeholder";
Credential cred = new Credential();
try { cr = cred.execute().get(); }
catch (InterruptedException e) { e.printStackTrace(); }
catch (ExecutionException e) { e.printStackTrace(); }
//tv.setText(cr);
if (cr == "FALSE") {
Log.d("Checking in false", cr);
Toast.makeText(Credentials.this, "Cannot log in: wrong username or password", Toast.LENGTH_LONG).show();
startActivity(new Intent(Credentials.this, MainActivity.class));
} else if (cr != "FALSE") {
Log.d("Checking in true", cr);
Intent intent = new Intent(Credentials.this, ProfileActivity.class);
intent.putExtra(INTENT_DATA, cr);
startActivity(intent);
}
}
});
By using the Log.d() static method and looking at the LogCat, I'm sure that the returned value is indeed FALSE if the credentials are wrong (again, it's not a boolean value, just String in all Caps). The funny thing is that the code that gets executed is the "else if" part of the snippet I have posted but it actually logs that the returned string value is FALSE! Which means (at least to me) that it should have executed the first "if" condition (that is, if (cr == "FALSE") { } ). But no, it keeps executing the "else if" part.
Any ideas?
Use equals for string comparisons.
I.E:
WRONG:
if (cr == "FALSE") {
CORRECT:
if (cr.equals("FALSE")) {
Use
if (cr.contentEquals("false") {
//your code
} else {
//your code
}