EditText - not able to fetch the values - android

public boolean saveTheUpdate(int position)
{
System.out.println("In update Save Method");
String strOut=objEditText.getText().toString();
if(strOut !=null && strOut.length() !=0 && arrlstCo_ordinate.size() !=0)
{
mapDefect.put(objEditText.getId(),strOut);
Log.d("Err", "Map Size :"+mapDefect.size() +"Arr List Size :"+arrlstCo_ordinate.size());
db.updateDefectDescription(arrlstCo_ordinate, mapDefect,position);
Toast.makeText(FragmentActivity.this, "Defect updated", Toast.LENGTH_SHORT).show();
count=1;
removeLocalView();
fechCoordinate();
addViewEditText();
return true;
}else
{
Toast.makeText(FragmentActivity.this, "Please log the defect before saving", Toast.LENGTH_SHORT).show();
return false;
}
}
So, I have an EditText open. The data inside this is stored in SortedMap - mapDefect. This is used later to insert in a database. However, at random times String strOut=objEditText.getText().toString(); is not working.
There are actually many EditTexts. It fetches information from the wrong EditText even though they have been invisible. The data is to be picked from the EditText that is currenytly visble. It works fine sometimes and sometimes it doesn't - It fetches form the correct EditText sometimes and sometimes not.

if(!strOut.equalsIgnoreCase("")&& strOut.length() !=0 && arrlstCo_ordinate.size() !=0)
{
}
replace with
if(strOut !=null && strOut.length() >0 && arrlstCo_ordinate.size() >0)
{
}

Related

If statement, Android Studio with Firebase

So I am trying to send data back to my Firebase Database with only users that have signed up and logged in. I have written a code to send the data but i am trying to write an else if statement that a message in this case a Toast appears if they have not signed up. I am struggling to write a code for that.
private void sendScore() {
String userName = name.getText().toString();
String userScore = BenchScore.getText().toString();
if(!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(userScore)) {
String id = databaseReference.push().getKey();
ScoreProfile scoreProfile = new ScoreProfile(id, userName, userScore);
databaseReference.child(FirebaseAuth.getInstance().getUid()).setValue(scoreProfile);
name.setText("");
BenchScore.setText("");
} else if(FirebaseAuth.getInstance().) {
Toast.makeText(Score.this, "You need to sign up in order to use this", Toast.LENGTH_SHORT).show();
}
}
If you want to check if an user is logged in you can use :
...
else if( FirebaseAuth.getInstance().getCurrentUser() == null )
{
// Call your toast here
}
Replace ur code with this one:
private void sendScore() {
String userName = name.getText().toString();
String userScore = BenchScore.getText().toString();
if(FirebaseAuth.getInstance().getUid() != null && !TextUtils.isEmpty(userName) && !TextUtils.isEmpty(userScore)){
String id = databaseReference.push().getKey();
ScoreProfile scoreProfile = new ScoreProfile(id, userName, userScore);
databaseReference.child(FirebaseAuth.getInstance().getUid()).setValue(scoreProfile);
name.setText("");
BenchScore.setText("");
} else if (FirebaseAuth.getInstance().getUid() == null) {
Toast.makeText(Score.this, "You need to sign up in order to use this", Toast.LENGTH_SHORT).show();
}
}
I've just included a check for whether getUid() is non null in the if statement and in else if, I've checked whether getUid() is null. This would serve your purpose, hopefully.

Mandatory fields in android [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 1 year ago.
Hello I am making an app where user can order some books. When he, or she, fill out the order with his firs second name etc and hits order button applications,using Intent, will switch to SMS and thus will purchase books via text message.But i wish to be able, if user accidently forget to fill up all fields, toast pop up with message "Please fill up XYZ field". I used if else, but when some field remain empty something else happened.I got android message that my app needs to be closed and and return me to the previous activity.In my LogCat nothing happened . No error message.
this is my code :
public void kreiranjeNarudzbine(View v) {
EditText editTextIme = (EditText) findViewById(R.id.ime);
String imeNarucioca = editTextIme.getText().toString();
EditText editTextPrezime = (EditText)findViewById(R.id.prezime);
String prezimeNarucioca = editTextPrezime.getText().toString();
EditText editTelefonNarucioca = (EditText)findViewById(R.id.telefon);
String telefonNarucioca = editTelefonNarucioca.getText().toString();
EditText editAdresaNarucioca = (EditText)findViewById(R.id.adresa);
String adresaNarucioca = editAdresaNarucioca.getText().toString();
EditText editGradNarucioca = (EditText)findViewById(R.id.grad);
String gradNarucioca = editGradNarucioca.getText().toString();
EditText editKolicina = (EditText)findViewById(R.id.kolicina);
String narucenaKolicina = editKolicina.getText().toString();
int kolicina = Integer.parseInt(narucenaKolicina);
int cenaNarudzbine = cena(kolicina);
String poruka = sumiranjeNarudzbine(imeNarucioca, prezimeNarucioca,telefonNarucioca,adresaNarucioca,gradNarucioca,cenaNarudzbine);
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "+381629647169");
smsIntent.putExtra("sms_body",poruka);
if(imeNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();
}
else if(telefonNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina.equals("")){
Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
startActivity(smsIntent);}
}
As already mentioned, you check to make sure it isn't null or empty.
if(imeNarucioca!=null && imeNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca!=null && prezimeNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();
}
else if(telefonNarucioca!=null && telefonNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca!=null && adresaNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca!=null && gradNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina!=null && narucenaKolicina.equals("")){
Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
startActivity(smsIntent);
}
This can be converted into a method to prevent as much text:
public boolean isETEmpty(EditText et){
return (et != null && (et.equals("") || et.equals(" ")));//the final piece checks if the edittext is empty or just contains a space. It is or between
//in order to ensure that one of those are true. Thus the parenthesis
}
(how the above works)
Now, for the required fields you can use HTML to format the text:
/*your view*/.setText(Html.fromHtml("Your title. <font color='red'>*</font>"));
The above code formats the code by adding a red star after the required fields. Later down you can do this:
/*your view*/.setText(Html.fromHtml("<font color='red'>*</font> Required field"));
The comment /*your view*/ you replace with a textview reference, edittext or whatever you use to set text
Further reading:
https://stackoverflow.com/a/9161958/6296561
The string of empty edittext might be null, which causes the issue. Try the following code.
if(imeNarucioca==null || imeNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca==null || prezimeNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();
}
else if(telefonNarucioca==null || telefonNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca==null || adresaNarucioca.equals("")){
Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca==null || gradNarucioca.equals("")){
Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina==null || narucenaKolicina.equals("")){
Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
startActivity(smsIntent);}
}
You should use isEmpty method provided in android.text.TextUtils. The whole description and implement of this method:
/**
* Returns true if the string is null or 0-length.
* #param str the string to be examined
* #return true if str is null or zero length
*/
public static boolean isEmpty(#Nullable CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
So, to validate null or empty user input, you can do something like this:
if(android.text.TextUtils.isEmpty(imeNarucioca)){
// ...
}
So i'm using this, you can do with it what you like:
This is the method:
public static boolean checkEditTextIsEmpty(EditText... editTexts)
{
try
{
for (EditText editText : editTexts)
{
if (editText.getText().toString().trim().length() == 0)
{
Drawable d = _application.currentActivity.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
editText.requestFocus();
editText.setError(editText.getHint() + " is empty", d);
return false;
}
}
}
catch (Exception ignored)
{
return false;
}
return true;
}
This is a preview: (ignore my style and background)
This is how i implemented it:
if(checkEditTextIsEmpty(txtEmail, txtPassword))
{
//Do whatever if EditTexts is not empty
}
Just add this line in your Java file pro-grammatically,
adding mandatory field near Your Text view.
tv.setText (Html.fromHtml(YourText
+ " <font color='"
+ getResources().getColor(R.color.colorAccent) + "'>" + " * "
+ "</font>"));
add Required field instead of yourText field,
then use the required color from colors.xml

How to test string for null value

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(""))
)

Save data in Edit Text in android

I have 5 Edit Texts in android for users to input number and save . I would like to know if I could have a method for checking if any Edit Texts in already number,next number display another edit text ,but every time it override first edit text value ,Please tell me how can check edit text box not blank and number display another edit text.
number1=(EditText)findViewById(R.id.editText1);
number2=(EditText)findViewById(R.id.editText2);
number3=(EditText)findViewById(R.id.editText3);
number4=(EditText)findViewById(R.id.editText4);
number5=(EditText)findViewById(R.id.editText5);
Intent i=getIntent();
Bundle bundle = getIntent().getExtras();
String number = i.getStringExtra("Number");
Log.e("international", "number1"+number);
if((number1.getText().toString().equals("")))
{
number1.setText(number);
Log.e("international", "number1"+number);
}
else if ((!number1.getText().toString().equals("")) && number2.getText().toString().equals(""))
{
number2.setText(number);
Log.e("international", "number2"+number);
}
else if ((number1.getText().length()!=0) && (number2.getText().length()!=0) && number3.length()==0 )
{
number3.setText(number);
Log.e("international", "number3"+number);
}else if (number1.length()!=0 && number2.length()!=0 && number3.length()!=0 && number4.length()==0 )
{
number4.setText(number);
}else if (number1.length()!=0 && number2.length()!=0 && number3.length()!=0 && number4.length()!=0 && number5.length()==0 )
{
number5.setText(number);
}else
{
number3.setText(number);
Log.e("international else...", "number3"+number);
}

Method returns wrong value - Android

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)

Categories

Resources