Toast appearing at inappropriate time - java

I have created if else statement in which if the user is registered, toast will display "User already registered", otherwise it will register the user and another toast will display "User registered successfully". But when i hit register button, both the toasts appear instead of just "user registered successfully". Tell me how the first toast should not appear with it. Below is my code.
if (dataSnapshot.child(username2et.getText().toString()).exists()) {
mDialog.dismiss();
Toast.makeText(RegisterScreen.this, "Username already registered", Toast.LENGTH_SHORT).show();
} else {
mDialog.dismiss();
User user = new User(emailet.getText().toString(), password2et.getText().toString(), phonenoet.getText().toString(), carplatenoet.getText().toString());
table_user.child(username2et.getText().toString()).setValue(user);
Toast.makeText(RegisterScreen.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
finish();
}

Related

Firestore Database Registration Failed Permission Denied But Actually Successfully Registers

So I am having a rather ambiguous issue with my database. Everything was working fine and anytime I would register or somebody would register on my app, it would make the messasge "Registration Successful!" pop up.
I have been updating my code daily and working on changing things in the app and around the database. Today while registering a test user, I got this message -
It claims that registration failed, but the account was successfully registered. And I have tested this with 3 more accounts. Same message - registration, however, is actually successful and the user can log in with his details.
I have not coded this exception. I have coded exceptions such as password too short, registration failed, no internet, etc, but never have I created a Permission check. I think the Permission Denied comes from the +e.getMessage()? Could I have changed something inside the firestorm database rules?
This is my code:
private void showREGISTERnew() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use email to register.");
LayoutInflater inflater = LayoutInflater.from(this);
View layout_login = inflater.inflate(R.layout.layout_login, null);
final MaterialEditText editEmail = layout_login.findViewById(R.id.editEmail);
final MaterialEditText editPassword = layout_login.findViewById(R.id.editPassword);
dialog.setView(layout_login);
//set btton
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//check validation
if(TextUtils.isEmpty(editEmail.getText().toString())){
Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(editPassword.getText().toString())){
Snackbar.make(rootLayout, "Please enter your password", Snackbar.LENGTH_SHORT)
.show();
return;
}
if(editPassword.getText().toString().length() < 6){
Snackbar.make(rootLayout, "Password too short.", Snackbar.LENGTH_SHORT)
.show();
return;
}
//reg new user
auth.createUserWithEmailAndPassword(editEmail.getText().toString(), editPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
//save user to db
User user = new User();
user.setEmail(editEmail.getText().toString());
//user setphone
//user setname
user.setPassword(editPassword.getText().toString());
//use email to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "You have registered successfully.", Snackbar.LENGTH_SHORT)
.show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout, "Registration failed."+e.getMessage(), Snackbar.LENGTH_SHORT)
.show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Snackbar.make(rootLayout, "Registration failed."+e.getMessage(), Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
I actually implemented two databases. One that will hold the userbase ad one that will hold price and location data.
However, I forgot that after a certain period of time, read and write permissions will be automatically set to false.
When I got the notification weeks ago, I changed back the read and write permissions to true on the main database, but forgot about the second one.
I checked the second one and the permissions were set to false, thus giving me registration failed, permission denied even though I could register.
After changing them back to true, application works flawlessly.

Toast is not showing- emulated with genymotion

I'm trying to develop an android app with android studio 2.3.3 and genymotion 2.10.0
I'm using toast msg to show the firebase connection result but it's not displayed despite there are no errors.
here is my code
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed !",
Toast.LENGTH_SHORT).show();
setContentView(R.layout.failure);
finish();
}
else{
setContentView(R.layout.success);
Intent settings = new Intent(LoginActivity.this,SettingsActivity.class);
startActivity(settings);
finish();
}
}
});
It doesn't show because you finish the Activity after the toast
you can change your ActivityContext to applicationContext
Toast.makeText(getApplicationContext(), "Authentication failed!" , Toast.LENGTH_SHORT).show();

mToast.cancel() is not working

I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless

Show toast only if it's different from the existing toast

So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).
To work towards this goal, I found this post on how to only show a toast if none are being shown.
I have modified the code to fit both requirements.
private Toast toast;
public void showAToast (String st, boolean isLong){
try{
toast.getView().isShown();
String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
if(!text.equalsIgnoreCase(st)){
//New message, show it after
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
} catch (Exception e) {
//New message
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
}
My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.
Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.
The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.
So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.
I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
In the OnCreate:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
//And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
// If you need to just cancel an existing toast (before it times out) use:
toast0.cancel();
toast1.cancel();
Quotation
You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.
Toast mToast;
public void showToast(CharSequence message, int during){
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), message, during);
} else {
mToast.setText(message);
}
mToast.show();
}
--↓---↓----↓---update--↓----↓---↓--↓
Sorry about that I miss your point above.
I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.
But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.

Java Android Toast not showing after boolean declaration

The toast that says not reachable is not showing up, but all the above are showing up, why does my code stop execution on that line?
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}
I copied this code into my test project and added a toast to the if and the else if for gpsEnabled and netOn.
It looks like the first if is the one that is getting executed as gpsEnabled is true.
Hope this helps, good luck.
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
Imloca returns null.Problem is this.

Categories

Resources