How to ensure only even numbers are entered in edittext in textwatcher? - android

I need to enter only even numbers(range:0-20) in edittext in textwatcher. However, when i did the following, it remained same that is it is allowing both even and odd numbers to be entered. I want when a user enters an odd number the toast is displayed. guide me please!
even
#Override
public void afterTextChanged(Editable s) {
try {
int v = Integer.parseInt(s.toString());
if(v%2!=0){
if (v > 20) {
s.replace(0, s.length(), "", 0, 2);
} else if (v < 0) {
s.replace(0, s.length(), "", 0, 1);
}
}
} catch (NumberFormatException ex)
{
Toast.makeText(MainActivity.this, "invalid", Toast.LENGTH_LONG).show();
}
}

You are just showing the Toast when a NumberFormatException exception is thrown, meaning that the string does not contain a number.
So you have to show the toast also when the number is even, and you will do it on the else condition of
if(v%2 != 0){
//Number is odd
}else{
//Number is even
}
Try like the following:
#Override
public void afterTextChanged(Editable s)
try {
int v = Integer.parseInt(s.toString());
if(v>0 && v<20){
if(v%2 != 0){
Toast.makeText(getContext(), "ODD", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getContext(), "EVEN", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getContext(), "OUT OF RANGE", Toast.LENGTH_SHORT).show();
}
}catch (NumberFormatException e){
Toast.makeText(getContext(), "INVALID", Toast.LENGTH_SHORT).show();
}
}

Related

didn,t work checking between password and repeat password in android

........................................
else if (e8.getText().toString().length() == 0) {
e8.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
i = 0;
}
else if (e9.getText().toString().length() == 0) {
e9.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
j = 0;
}
else if (!e8.equals(e9.getText()))
{
e9.setError("Both Passwordsxmvbxb are different");
Toast.makeText(getApplicationContext(), "Please enter Correct Password", Toast.LENGTH_SHORT).show();
k = 0;
}
........................................
Try This
}
else if (e9.getText().toString().equals("")) {
e9.setError("Password is required");
Toast.makeText(getApplicationContext(), "Please enter Password", Toast.LENGTH_SHORT).show();
j = 0;
}
else if (!e8.getText().toString().equals(e9.getText().toString()))
{
e9.setError("Both Passwordsxmvbxb are different");
Toast.makeText(getApplicationContext(), "Please enter Correct Password", Toast.LENGTH_SHORT).show();
k = 0;
}
try this approach.
if(validate()){
// perform operation
}
method declaration:
private boolean validate()
{
if (TextUtils.isEmpty(edtPassword.getText())) {
//toast enter password
return false;
} else if (TextUtils.isEmpty(edtConfirmPassword.getText())) {
//toast enter confirm password
return false;
} else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString())) {
//toast password not match
return false;
}
return true; // considering all conditions are true
}
It should be String when you are checking any two string. currenty you are doing e8.equal where e8 is your EditText so it will check for equal object. It should be
if(!e8.getText().toString().equals(e9.getText().toString()))
use .equals instead of == to compare string .

How to check validate in android? and if both intime and outtime are empty then Toast or both of one empty then passed

if both intime and outtime are empty then Toast or both of one empty then passed
private boolean isValidate() {
// TODO Auto-generated method stub
if(sp.getSelectedItem().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Name !", Toast.LENGTH_LONG).show();
return false;
}else{
}
if(etDate.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Date !", Toast.LENGTH_LONG).show();
return false;
}else{
}
//both intime and out time passed either one of passed here
if(etintime.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter In Time !", Toast.LENGTH_LONG).show();
return false;
}else{
}
if(etouttime.getText().toString().trim().length() == 0){
Toast.makeText(getApplicationContext(), "Please Enter Out Time !", Toast.LENGTH_LONG).show();
return false;
}else{
}
return true;
}
Use && conditional operator to perform logical And, in your case check both of your EditTexts if they are empty.
e.g.
if(editText1.getText().toString().equals("") && editText2.getText().toString().equals("")){
//show Toast here.
}

not work condition onCheckedChanged

I have an issue with the following condition:
if (Cuadrado.isChecked() == true)
This is the full code:
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String dato5 = DatoMath.getText().toString();
if (Cubo.isChecked()==true) {
if (dato5.equals("")) {
Toast toast = Toast.makeText(getApplicationContext(), "no hay datos", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER | Gravity.LEFT, 0, 0);
toast.show();
} else {
int datofinal = Integer.parseInt(dato5);
int final3 = (int) Math.pow(datofinal, 2);
Resultado.setText("" + final3);};
if (Cuadrado.isChecked() == true) {
if (dato5.equals("")) {
Toast toast = Toast.makeText(getApplicationContext(), "no hay datossssss", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT, 0, 0);
toast.show();
} else {
int datofinal2 = Integer.parseInt(dato5);
int final4 = (int) Math.pow(datofinal2, 3);
Resultado.setText("" + final4);
};
#Override
public void onCheckedChanged(RadioGroup group, int checkedId){
switch(checkedId){
case R.id.rbtn1:
// radio button 1 checked related code
break;
case R.id.rbtn2:
// radio button 2 checked related code
break;
}
}
Why are you intentionally complicating your code, you can simply write:
if (Cubo.isChecked())
{
//do whatever you want to do
}
The isChecked method returns a boolean so you can directly write it inside that if condition
And one more thing, why have you put a semi-colon[ ; ] after your else statements closing brace[ } ], replace that semicolon with another closing brace[ } ]

password minimum and maximum character

I'm trying to have the maximum character of password that i want to set in my android apps. What would be the best way to code it ? I only have the minimum of 6 character and i want to have maximum of 10 character set password. Here is my attempt:
public void UserSignUp() {
if(ET_PASSWORD_SIGNUP.getText().length() >0 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >0 &&
ET_PASSWORD_SIGNUP.getText().length() >5 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >5)
{
if(ET_PASSWORD_SIGNUP.getText().toString().equals(ET_CONFIRM_PASSWORD_SIGNUP.getText().toString())) {
String SignUpResult = "";
db = DBMANAGER.getWritableDatabase();
SignUpResult = CONTENTMANAGER.saveUserPassword(ET_PASSWORD_SIGNUP.getText().toString(),this);
Toast.makeText(getApplicationContext(), SignUpResult, Toast.LENGTH_SHORT).show();
ActivityLogIn();
} else {
Toast.makeText(getApplicationContext(), "Confirm Password and Password did not match!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_SHORT).show();
}
}
Not an answer as such but another solution,
Have a look at https://github.com/ragunathjawahar/android-saripaar, nice annotation library.
You can annotate your password fields and it will handle the logic for you...
#Password(order = 1, minLength = 4, maxLength = 10)
private EditText passwordEditText;
#ConfirmPassword(order = 2)
private EditText confirmPasswordEditText;
private boolean checkPW(String pw, String pw2) {
if(!pw.equals(pw2)){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pws_do_not_match), Toast.LENGTH_LONG).show();
return false;
}
if (pw.length() < 4) {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_short), Toast.LENGTH_LONG).show();
return false;
}
if(pw.length() > 10){
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_long), Toast.LENGTH_LONG).show();
return false;
}
//continue here
return true;
}
});
}
Check this code..
if (ET_PASSWORD_SIGNUP.getText().toString().length() < 6
|| ET_PASSWORD_SIGNUP.getText().toString().length() > 10) {
ET_PASSWORD_SIGNUP.requestFocus();
ET_PASSWORD_SIGNUP.setError("Password should be between 6 to 10 characters long");
}
you can write this code:
// check password
public boolean checkPassword(String password, String confirmPassword) {
boolean state = false;
int passLength = password.length();
if (passLength >= 6) {
if (confirmPassword.length() != 0) {
if (password.equals(confirmPassword)) {
state = true;
} else {
makeToast("Password does't match!");
inputConfirmPassword.setText("");
inputPassword.setText("");
inputPassword.requestFocus();
}
} else {
Toast.makeText(this, "Please enter confirm password",
Toast.LENGTH_SHORT).show();
inputConfirmPassword.setText("");
inputConfirmPassword.requestFocus();
state = false;
}
} else {
Toast.makeText(this, "Please enter 6 digit password",
Toast.LENGTH_SHORT).show();
inputPassword.setText("");
inputPassword.requestFocus();
state = false;
}
return state;
}

Program Execution is not in proper way

When I click on button in my program without entering data it shows both errors parallely (I have Highlighted in below prog). here i need to get only one error at a time i mean when it is null it has show appropriate one.vice versa..
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v==findViewById(R.id.button1)) {
et1 = (EditText) findViewById(R.id.editText1);
if(et1.getText()!=null ) {
try {
radius = Double.valueOf(et1.getText().toString());
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Please enter correct value", Toast.LENGTH_SHORT).show();
}
}
if(radius==0.0) {
Toast.makeText(getApplicationContext(), "Value cannot be 0", Toast.LENGTH_SHORT).show();
}
try {
output = (double) Math.round(Math.PI * radius * radius);
String s = Double.toString(output);
tv1.setText(s);
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Please enter correct value", Toast.LENGTH_SHORT).show();
}
}
}
});
do not make things more complicated as they actually are. You can be sure that a correct value was entered without any try/catch blocks. Possible approach:
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Pattern p = Pattern.compile("([0-9]*)");
if (et1.getText().toString().trim().length() > 0) {
Matcher m = p.matcher(et1.getText().toString().trim());
if (m.matches()) {
radius = Double.valueOf(et1.getText().toString());
output = (double) Math.round(Math.PI * radius
* radius);
tv1.setText(Double.toString(output));
} else
Toast.makeText(getApplicationContext(),
"incorrect value", Toast.LENGTH_SHORT)
.show();
} else
Toast.makeText(getApplicationContext(),
"input is empty", Toast.LENGTH_SHORT).show();
break;
default:
}
}
});
In the above code you check if there was any text entered. The second if checks whether the input is numeric using a java regex. When both requirements are met you can be sure the output is calculated correctly.
BTW, using switch-case is a better approach for click listeners

Categories

Resources