So basically I have created a login form in I have validations and intent ..but the problem coming is the page is getting intended ....but validations are not working .....like if i will levae the form empty the toast msg is displaying but the page is also getting intended....I don't want like this ....I want if all the fields are empty then the page should not intent and should shoe validation.
this is my NewUserActivity.java code
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class NewUserActivity extends AppCompatActivity {
EditText name;
EditText email;
EditText phone;
EditText usname;
EditText passsword;
Button register;
ToastManager toastManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_user_login);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
usname = (EditText) findViewById(R.id.usname);
passsword = (EditText) findViewById(R.id.passsword);
register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String NAME = name.getText().toString().trim();
String EMAIL = email.getText().toString().trim();
String PHONENO = phone.getText().toString().trim();
String username = usname.getText().toString().trim();
String password = passsword.getText().toString().trim();
String emailPattern = "^[a-zA-Z0-9+_.-]{3,32}+@[a-zA-Z0-9.-]{2,32}+$";
String phonePattern = "(0/91)?[7-9][0-9]{9}";
boolean isAtLeastOneEditTextNotEmpty = !NAME.isEmpty()
|| !EMAIL.isEmpty()
|| !PHONENO.isEmpty()
|| !username.isEmpty()
|| !password.isEmpty();
ToastManager toastManager = new ToastManager(NewUserActivity.this);
if (isAtLeastOneEditTextNotEmpty) {
// NAME VALIDATION
if (NAME.isEmpty()) {
toastManager.addToast("ENTER NAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((NAME.length() > 3) && (NAME.length() < 15))) {
toastManager.addToast("NAME IS TOO SHORT.IT MUST BE BETWEEN 3-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
} else if (!NAME.matches("[a-zA-Z ]+")) {
toastManager.addToast("ONLY ALPHABETS ALLOWED", ToastManager.Duration.LENGTH_SHORT);
}
//EMAIL VALIDATION
if (EMAIL.isEmpty()) {
toastManager.addToast("ENTER EMAIL-ID", ToastManager.Duration.LENGTH_SHORT);
} else if (!(EMAIL.matches(emailPattern))) {
toastManager.addToast("INVALID EMAIL", ToastManager.Duration.LENGTH_SHORT);
}
//PHONE NUMBER VALIDATION
if (PHONENO.isEmpty()) {
toastManager.addToast("ENTER PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
} else if (!(PHONENO.length() == 10)) {
toastManager.addToast("INVALID PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
} else if (!(PHONENO.matches(phonePattern))) {
toastManager.addToast("INVALID PHONE NO.", ToastManager.Duration.LENGTH_SHORT);
}
//USERNAME VALIDATION
if (username.isEmpty()) {
toastManager.addToast("ENTER USERNAME", ToastManager.Duration.LENGTH_SHORT);
} else if (!((username.length() > 6) && (username.length() < 15))) {
toastManager.addToast("USERNAME IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
//PASSWORD VALIDATION
if (password.isEmpty()) {
toastManager.addToast("ENTER PASSWORD", ToastManager.Duration.LENGTH_SHORT);
} else if (!((password.length() > 6) && (password.length() < 15))) {
toastManager.addToast("PASSWORD IS TOO SHORT.IT MUST BE BETWEEN 6-15 CHARACTERS.", ToastManager.Duration.LENGTH_SHORT);
}
} else {
toastManager.addToast("ALL FIELDS ARE COMPULSORY", ToastManager.Duration.LENGTH_SHORT);
}
// Finally show all toast all screen
toastManager.show();
}
});
Intent p = new Intent(NewUserActivity.this,MainActivity2.class);
startActivity(p);
}
}
and this is my new_User_login.xml page
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
tools:context=".NewUserActivity"
android:background="@drawable/ic_launcher_background"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView4"
android:layout_width="276dp"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="106dp"
android:layout_marginBottom="581dp"
android:text=" REGISTRATION "
android:textColor="@color/WhiteSmoke"
android:textSize="30dp" />
<EditText
android:id="@+id/name"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="36dp"
android:layout_marginBottom="487dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="120dp"
android:layout_marginBottom="530dp"
android:drawableLeft="@drawable/ic_action_name"
android:text="NAME"
android:textColor="@color/WhiteSmoke"
android:textSize="20dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="195dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="138dp"
android:layout_marginBottom="440dp"
android:drawableLeft="@drawable/ic_action_mail"
android:text="EMAIL ID"
android:textColor="@color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="@+id/email"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="36dp"
android:layout_marginBottom="388dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="186dp"
android:layout_marginBottom="351dp"
android:drawableLeft="@drawable/ic_action_phone"
android:text="PHONE NO."
android:inputType="number"
android:textColor="@color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="@+id/phone"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="36dp"
android:layout_marginBottom="306dp" />
<TextView
android:id="@+id/textView8"
android:layout_width="195dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="135dp"
android:layout_marginBottom="254dp"
android:drawableLeft="@drawable/ic_action_username"
android:text="USERNAME"
android:textColor="@color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="@+id/usname"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="36dp"
android:layout_marginBottom="205dp" />
<TextView
android:id="@+id/textView9"
android:layout_width="195dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="131dp"
android:layout_marginBottom="165dp"
android:drawableLeft="@drawable/ic_action_password"
android:text="PASSWORD"
android:textColor="@color/WhiteSmoke"
android:textSize="20dp" />
<EditText
android:id="@+id/passsword"
android:layout_width="295dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="36dp"
android:layout_marginBottom="113dp" />
<Button
android:id="@+id/register"
android:layout_width="324dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="13dp"
android:layout_marginBottom="23dp"
android:background="@color/DeepPink"
android:fontFamily="sans-serif"
android:text="REGISTER"
android:textColor="@color/Black"
android:textSize="24dp" />
</RelativeLayout>
this is my Toastmanager.class
public class ToastManager {
private final WeakReference<Context> mContext;
private final Handler uiHandler = new Handler(Looper.getMainLooper());
private final List<Item> items = new ArrayList<>();
private int durationInMillis;
private boolean isShowing;
private int delayedBetweenToastInMillis;
public ToastManager(Context context) {
mContext = new WeakReference<>(context);
}
public void addToast(String message, @NonNull Duration duration) {
Item item = new Item(message, duration);
items.add(item);
}
public void show() {
// Prevent client from calling this method many time.
if (isShowing) {
return;
}
// Show all toast on screen.
showToast();
// After calling show(), if client add new toasts by calling addToast()
// Then we must show them on screen. Otherwise reset all data of this class.
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (!items.isEmpty()) {
showToast();
} else {
reset();
}
}
}, durationInMillis);
}
public void setDelayedBetweenToast(int delayInMillis) {
delayedBetweenToastInMillis = delayInMillis;
}
public void cancel() {
reset();
uiHandler.removeCallbacksAndMessages(null);
}
private void showToast() {
List<Item> list = new ArrayList<>(items);
items.clear();
durationInMillis = 0;
for (final Item item : list) {
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext.get(), item.text, item.getDurationForToast()).show();
}
}, durationInMillis);
durationInMillis += item.getDurationInMillis() + delayedBetweenToastInMillis;
}
}
private void reset() {
items.clear();
durationInMillis = 0;
isShowing = false;
}
private static class Item {
String text;
Duration duration;
Item(String text, Duration duration) {
this.text = text;
this.duration = duration;
}
int getDurationForToast() {
return duration == Duration.LENGTH_SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
}
int getDurationInMillis() {
return duration == Duration.LENGTH_SHORT ? 2000 : 3500;
}
}
enum Duration {
LENGTH_SHORT,
LENGTH_LONG
}
}