Why does this method always return true, even when the editTexts have nothing in them?
private boolean allFieldsFilledOut() {
boolean allFieldsFilledOut = false;
EditText name = (EditText) findViewById(R.id.editTextName);
EditText passWord = (EditText) findViewById(R.id.editTextPassWord);
EditText image = (EditText) findViewById(R.id.editTextProfilePicture);
if (name.getText().toString() != "" && passWord.getText().toString() != ""
&& image.getText().toString() != "") {
allFieldsFilledOut = true;
}
else {
allFieldsFilledOut = false;
}
return allFieldsFilledOut;
}
One thing I have been wondering about is, I create this activity through an intent a few times per use case. Am I referencing an old activity's editTexts? Should I be killing the activity when show a new activity? findViewById(R.id.editTextName) gets the application resource, with no reference to this specific activity. Is there another way to reference these editTexts?
When you compare string, use equals is compare string's value, use == is compare string's address
this mean (name.getText().toString())'s address != ("")'s address so that it always true
name.getText().toString() != ""
if you want to compare string's value, you need to use equals
if (name.getText().toString().equals("") == false &&
passWord.getText().toString().equals("") == false &&
image.getText().toString().equals("") == false)
Related
i have created a app with 3 edit text and and 3 buttons,
Edittext:
- id
- name
- date
Button:
submit.
check.
delete.
name to store name of user,
date to store date,
submit to store data in sqlit database,
check to retrive data,
delete to delete the specfied id in database,
id is auto incremented it is used when deleting.
my problem here is if i not enter anything in the edit text it takes null values
and display as shown in below pic instead of taking null i want to show message "PLEASE ENTER DATA"
and whenever i delete data next id value must decrease by 1 please help me.
Before entering the data to database use should check EditTexts are null or not in on click method and if null print toast messages like below
idEditText = (EditText) findViewById(R.id.editText_id);
nameEditText = (EditText) findViewById(R.id.editText_Name);
dateEditText = (EditText) findViewById(R.id.editText_Date);
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().();
String date = dateEditText.getText().toString().();
if(id.length() == 0 || name.length() == 0 || date.length())
{
Toast.makeText(this, "Please fill all details",Toast.LENGTH_SHORT).show();
}
Try this
idEditText =(EditText) findViewById(R.id.editText_id);
nameEditText =(EditText)findViewById(R.id.editText_Name);
dateEditText =(EditText)findViewById(R.id.editText_Date);
private boolean checkEmptyFields() {
boolean check = false
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().trim();
String date = dateEditText.getText().toString().trim();
if (id.isEmpty()) {
idEditText.setError("Invalid Input");
check = true;
} else {
dEditText.setError(null);
check = false;
}
if (name.isEmpty()) {
nameEditText.setError("Invalid Input");
check = true;
} else {
nameEditText.setError(null);
check = false;
}
if (date.isEmpty()) {
dateEditText.setError("Invalid Input");
check = true;
} else {
dateEditText.setError(null);
check = false;
}
return check;
}
if (!checkEmptyFields())
{
//Add Data to DB
}
I want to get value from several variables for example user1, user2, user3, user4.
how to check if variables are empty otherwise get the value and ignore variables that empty.
how I achieve this?, and sorry for newbie question...
if I do this
if(user1 != null && user2!= null && user3 != null && user4 != null){
user1.getText(); // or v1 = user1.value();
user2.getText(); // or v2 = user2.value();
user3.getText(); // or v3 = user3.value();
user4.getText(); // or v4 = user4.value();
}
which I dont want to do that, I just want to get the variable which had value in it and save it in array
Try with the following code.
ArrayList<String> listItems=new ArrayList<String>();
if(user1!=null)
{
listItems.add(user1.getText());
}
if(user2!=null)
{
listItems.add(user2.getText());
}
if(user3!=null)
{
listItems.add(user3.getText());
}
if(user4!=null)
{
listItems.add(user4.getText());
}
String [] arrayData = listItems.toArray(new String[listItems.size()]);
Note:You can also use isEmpty() method instead of checking for not a null value.
You mean a null check?
Value v1;
Value v2;
if (user1 != null) {
v1 = user1.value();
}
if (user2 != null) {
v2 = user2.value();
}
...etc
If you are dealing with String values then you may want to check the empty or null using TextUtils.isEmpty(str) API
public static boolean isEmpty (CharSequence str) Returns true if the string is null or 0-length.
if(user1!=null && !TextUtils.isEmpty(user1.getText()){
//not empty or null
}
Have a problem with this code!
I want to check the editText values, if it is null or not...
But it gets stuck at the if segment, doesnt mather if it is a value in the editText or not.
If there is a value in the editText string it should go further and calculate the values.
Second problem I have is the toast, it doesnt show the text in the string variable, it just prints the string link.
private EditText fp;
private EditText fC;
private EditText drive;
private TextView totalcost;
public void CalcButton(View button) {
// Converting strings to float and check if each is NULL (empty)
if (!(fp.getText().equals(null)) || (fC.getText().equals(null)) || (drive.getText().equals(null)))
{
Toast.makeText(getApplicationContext(), "#string/toast", Toast.LENGTH_LONG).show();
}else {
String n1 = fp.getText().toString();
float no1 = Float.parseFloat(n1);
String n2 = fC.getText().toString();
float no2 = Float.parseFloat(n2);
String n3 = drive.getText().toString();
float no3 = Float.parseFloat(n3);
// Calculates the floats
float calc = no1 * no2 * no3;
// Converting and prints out the result
String sum = Float.toString(calc);
totalcost.setText(sum);
}
You should not do it this way, do this instead:
if (!fp.getText().toString().equals("")) {
}
To problem with Toast - use this:
Toast.makeText(getApplicationContext(), R.string.toast, Toast.LENGTH_LONG).show();
Try to use TextUtils.isEmpty() instead, it checks for null and 0-length String.
On your if statement it should look like:
if (!TextUtils.isEmpty(fp.getText().toString())) {
// Code
}
And on your Toast, change "#string/toast" to R.string.toast or getApplicationContext().getString(R.string.toast);
The code should look like:
// Converting strings to float and check if each is NULL (empty)
if (! (TextUtils.isEmpty(fp.getText().toString()) ||
(TextUtils.isEmpty(fC.getText().toString())) ||
(TextUtils.isEmpty(drive.getText().toString()))))
{
Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.toast), Toast.LENGTH_LONG).show();
}
More on the getString() method here.
EDIT: I've seen that your code also was missing a pair of parenthesis ( ). So your "not" was only applying to the first test.
Something like:
!(test1) || test2 || test3
Instead of
!((test1) || (test2) || (test3))
To check if EditText is empty you do editText.getText().toString().equals("")
So, Your if-statement will look like this:
if (!(fp.getText().toString().equals("")) ||
(fC.getText().toString().equals("")) ||
(drive.getText().toString().equals("")))
And your Toast would be like this:
Toast.makeText(getApplicationContext(), R.string.toast, Toast.LENGTH_LONG).show();
function isNull(int resourceId, boolean getError){
EditText editText= (EditText) findViewById(resourceId);
String strEditText = String.valueOf(editText.getText());
if(TextUtils.isEmpty(strEditText)) {
if(getError) editText.setError("this is null!");
return true;
}else{
return false;
}
}
May be this block gives you a few clues about yours
about If Conditions;
In your 'IF conditions', parentheses seems less than the count it is necessary.
Try to add one more after (!)
I guess it should be like this;
for Negative
if (!(
(String.valueOf(fp.getText()).equals("")) ||
(String.valueOf(fC.getText()).equals("")) ||
(String.valueOf(drive.getText()).equals(""))
))
for Positive
if (
(String.valueOf(fp.getText()).equals("")) ||
(String.valueOf(fC.getText()).equals("")) ||
(String.valueOf(drive.getText()).equals(""))
)
I have a string (length 3-8) assigned to a variable (text). I want to check whether the 2nd and 3rd characters are NOT numeric (a letter or symbol or space..or anything other than numbers).
Elementary way to do this could be:
if(((text.charAt(1)-'0')>=0)&&(text.charAt(1)-'0')<10))||((text.charAt(2)-'0')>=0)&&(text.charAt(2)-'0')<10)))
{
//do nothing, since this means 2nd and/or 3rd characters in the string are numeric
}
else
{
// Your condition is met
}
You could also use REGEX's , if your checking is still more complicated.
Here is Another way to achieve this:
boolean isNumeric = true;
String test = "testing";
char second = test.charAt(1);
char third = test.charAt(2);
try {
Integer.parseInt(String.valueOf(second));
Integer.parseInt(String.valueOf(third));
} catch(NumberFormatException e) {
isNumeric = false;
}
System.out.println("Contains Number in 2nd and 3rd or both position: " + isNumeric);
You might make use of the String.IndexOf(String) method, like:
String digits = "0123456789";
String s2 = text.substring(2,3);
String s3 = text.substring(3,4);
boolean valid = (digits.indexOf(s2) > -1) && (digits.indexOf(s3) > -1);
I have an EditText in android for users to input their AGE. It is set an inputType=phone.
I would like to know if there is a way to check if this EditText is null.
I've already looked at this question: Check if EditText is empty. but it does not address the case where inputType=phone.
These, I've checked already and do not work:
(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.
Thank you for your help.
You can check using the TextUtils class like
TextUtils.isEmpty(ed_text);
or you can check like this:
EditText ed = (EditText) findViewById(R.id.age);
String ed_text = ed.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
//EditText is empty
}
else
{
//EditText is not empty
}
First Method
Use TextUtil library
if(TextUtils.isEmpty(editText.getText().toString())
{
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
Second Method
private boolean isEmpty(EditText etText)
{
return etText.getText().toString().trim().length() == 0;
}
Add Kotlin getter functions
val EditText.empty get() = text.isEmpty() // it == ""
// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""
With these, you can just use if (edittext.empty) ... or if (edittext.blank) ...
If you don't want to extend this functionality, the original Kotlin is:
edittext.text.isBlank()
// or
edittext.text.isEmpty()
EditText textAge;
textAge = (EditText)findViewByID(R.id.age);
if (TextUtils.isEmpty(textAge))
{
Toast.makeText(this, "Age Edit text is Empty", Toast.LENGTH_SHORT).show();
//or type here the code you want
}
I use this method for same works:
public boolean checkIsNull(EditText... editTexts){
for (EditText editText: editTexts){
if(editText.getText().length() == 0){
return true;
}
}
return false;
}
Simply do the following
String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);
I found that these tests fail if a user enters a space so I test for a missing hint for empty value
EditText username = (EditText) findViewById(R.id.editTextUserName);
EditText password = (EditText) findViewById(R.id.editTextPassword);
// these hint strings reflect the hints attached to the resources
if (username.getHint().equals("Enter your username") || password.getHint().equals("Enter Your Password")){
// enter your code here
} else {
// alls well
}
editText.length() usually works for me
try this one
if((EditText) findViewByID(R.id.age)).length()==0){
//do whatever when the field is null`
}
Java: Check if EditText is Empty
1) find the EditText
ourEditText = view.findViewById(R.id.edit_text);
2) Get String value of EditText
String ourEditTextString = ourEditText.getText().toString();
3) Remove all spaces
Incase user inputs only blank spaces.
String ourEditTextNoSpaces = ourEditTextString.replaceAll(" ","");
3) Check if empty
boolean isOurEditTextEmpty = ourEditTextNoSpaces.isEmpty();
Try this. This is the only solution I got
String phone;
try{
phone=(EditText) findViewByID(R.id.age)).getText().toString();
}
catch(NumberFormatException e){
Toast.makeText(MainActivity.this, "plz enterphone Number ", Toast.LENGTH_SHORT).show();
return;
}