Save data in Edit Text in android - 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);
}

Related

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

RxJava combineLatest - observable from changes

I create form with 15 inputs and I want observe those inputs to check if something was changed, I compare results of inputs with current userModel. How can I reduce that code because seems to be little bit complicated. It looks like that:
Observable.combineLatest(fieldsViewHolder.observableInputsChanges(), new FuncN<Boolean>() {
#Override
public Boolean call(Object... args) {
if(userModel != null) {
return args[0].toString().equals(userModel.getFirstName()) && args[1].toString().equals(userModel.getLastName()) && args[2].toString().equals(userModel.getEmail()) &&
args[3].toString().equals(userModel.getMobilePhone()) && args[4].toString().equals(userModel.getCompanyPlace()) && args[5].toString().equals(userModel.getCompanyName()) &&
args[6].toString().equals(userModel.getCountry()) && args[7].toString().equals(userModel.getCompanyPosition()) && args[8].toString().equals(userModel.getPhone()) &&
args[9].toString().equals(userModel.getPostalCode()) && args[10].toString().equals(userModel.getStreet1()) && args[11].toString().equals(userModel.getStreet2()) &&
args[12].toString().equals(userModel.getFirstName()) && args[13].toString().equals(userModel.getWebPage()) && args[14].toString().equals(userModel.getCity());
}
return args[0].toString().isEmpty() && args[1].toString().isEmpty() && args[2].toString().isEmpty() &&
args[3].toString().isEmpty() && args[4].toString().isEmpty() && args[5].toString().isEmpty() &&
args[6].toString().isEmpty() && args[7].toString().isEmpty() && args[8].toString().isEmpty() &&
args[9].toString().isEmpty() && args[10].toString().isEmpty() && args[11].toString().isEmpty() &&
args[12].toString().isEmpty() && args[13].toString().isEmpty() && args[14].toString().isEmpty();
}
});
you can create UserModel object with constructor (pass all args to it)
in constructor map every args to field you want. now you have another UserModel.
you can compare them by overriding equal for UserModel or you can convert them with Gson and compare String! I prefer first solution.
Update
for empty lines that you add after edit you can check like this:
for(int i = 0; i < 15; i++){
if(!args[i].toString().isEmpty()) {
return false;
}
}
return true;

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)

getFromLocation using GPS

Can anyone tell me what is going wrong here. I am trying to obtain the address using reverse geocoding..`
if (locationGPS != null) {
list = geocoder.getFromLocation(locationGPS.getLatitude(),
locationGPS.getLongitude(), 3);
if (list != null) {
if (list.size() > 0) {
strZipcode = list.get(0).getPostalCode();
strAdminArea = list.get(0).getAdminArea();
strLocality = list.get(0).getLocality();
strAddressLine = list.get(0).getAddressLine(0);
Log.d(TAG, "list of address: "+ list);
Log.d(TAG, "Data: "+ mobileDataEnabled);
Log.d(TAG, "Data: "+ mobile);
int count = 0;
while ((strZipcode == null || strAdminArea == null
|| strLocality == null || (strAddressLine == null || strAddressLine == "USA"))
&& count < list.size()) {
strZipcode = list.get(count).getPostalCode();
strAdminArea = list.get(count).getAdminArea();
strLocality = list.get(count).getLocality();
strAddressLine = list.get(count)
.getAddressLine(count);
count++;
}`
This thing works fine and gives out the right address. But sometimes it gives out null for the all of the values which i am trying to retrieve despite the fact that i have a check in place for null values.. Am i missing something here?
Reverse Geocoder does not always return a value. It sounds weird but this is the way it is. F0r example if you hit for address 5 times, you won't be lucky enough to get response for all hits. You may get 3 instead. So workaround may be, instead of hitting geocoder one at a time, try looping your request to 2 or 3 times or more, one of the requests will work hopefully.

EditText - not able to fetch the values

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)
{
}

Categories

Resources