TextUtils.isEmpty(regUserOtherCity.getText().toString()) doesnt work on button press - android

I have a spinner of cities with default value "Other City". If the user can't find his city, when he selects Other City, a textInput (Your city) is shown.
When the user enters his city name there and Clicks the button Register, the getText().toString() doesn't return the value. And also when the field is empty even though I have written validation code, it doesn't work.
Log.i("cities[0]", "("+cities[0]+")");
Log.i("regOtherCity", "("+(regUserOtherCity.getText().toString())+")");
if (userCity.equals(cities[0]) && TextUtils.isEmpty(regUserOtherCity.getText().toString()) )
{
error = true;
Log.i("checkerror", "("+cities[0]+")");
regUserOtherCity.setError("Enter Your City");
regUserOtherCity.requestFocus();
}
else if (userCity.equals(cities[0]))
userCity = regUserOtherCity.getText().toString();
Log.i("cities[0]", "("+cities[0]+")");
Log.i("regOtherCity", "("+userCity+")");
I have tried debugging by using Logcat, the output is here for the time when the user enters 'my city' in the text input.
I/cities[0]: (Other City)
I/regOtherCity: (my city)
I/cities[0]: (Other City)
I/regOtherCity: ()
Edited:-----------------------------
On inspection, I found out that the value returned by userCity is empty.
here is the code that gets value of userCity..for some reasons it's not working.
regCitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (String.valueOf(parent.getItemAtPosition(position)).equals(cities[0])) {
regOtherCityLayout.setVisibility(View.VISIBLE);
userCity = regUserOtherCity.getText().toString();
Log.i("onItemSel userCity", "("+userCity+")");
} else {
regOtherCityLayout.setVisibility(View.GONE);
userCity = String.valueOf(parent.getItemAtPosition(position));
}
}

You're assigning userCity the value of the EditText inside onItemSelected. So if the user select Other City, the EditText value is assigned to userCity in the listener instead of the value of the EditText.
Simply assign the spinner value and use the if condition to set the visibility of the EditText.
userCity = String.valueOf(parent.getItemAtPosition(position));
Should work fine when you press the button then.

Related

How to save string written in EditText to string - android studio

I'm developing an application that allows user to add his own word, it's category. I start with a spinner that has the words available before in sqlite db and Add new word. When he clicks on Add new word, the spinner changes into EditText box that allows him to enter a new word. Whenever I try to save the text written in Edittext to string it shows that string is defined as "Add new word" and not the value entered by the user. What should I do?
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getId()==R.id.spinword)
{
editword=parent.getItemAtPosition(position).toString();
if(editword.equals("Add new word"))
{
spinwrd.setVisibility(spinwrd.INVISIBLE);
edtwrd.setVisibility(edtwrd.VISIBLE);
flag = 1;
}
if(flag == 1)
{
editword.replace(editword,edtcat.getText().toString());
}
}
if(parent.getId() == R.id.spincat) {
editcategory = parent.getItemAtPosition(position).toString();
if (editcategory.equals("Add new category")) {
spincat.setVisibility(spincat.INVISIBLE);
edtcat.setVisibility(edtcat.VISIBLE);
editcategory = edtcat.getText().toString();
}
}
}
I tried equals method and replace method but in vain.
Not sure of what you want to do but i think you should try changing
editword.replace(editword,edtcat.getText().toString()
for
editword.replace(editword,edtwrd.getText().toString())
The answer to this question is as follows: First, replacing, clearing, or any change depends on the position of the line meaning that: In the code above, I was trying to capture the text written by the user but at that point of execution when user chooses "Add new word" the text becomes it. The solution to this problem is to place the line of code editword.replace(editword,edtcat.getText().toString()); when this function is finished to be sure that the user has entered some text.

Retain edit text cursor position using data binding in android

I have created a getter a setter for my building name and wish change the string to replace any characters that does not match with the regex expression. However in doing so, every time I enter an invalid character the edit text cursor/selection position changes to the beginning of the text. Hoe do I avoid this from happening?
private String buildingname="";
#Bindable
public String getBuildingname() {
return this.buildingname;
}
public void setBuildingname(String buildingname) {
if(!this.buildingname.equals(buildingname)) {
this.buildingname = buildingname.replaceAll(alphanumericregex,"");
this.pcr.notifyChange(this, com.tomtom.sangrahit.BR.buildingname);
}
}
You can use setSelection of editText AFTER you are setting new text
int position = myEditText.getSelectionStart();
myEditText.setText(myNewText);
myEditText.setSelection(position);

Textfield duplicates text

I got a textfield that the user can write text in, and the text is saved into preferences so that the text remains the same as they left it. But I´ve must´ve done something wrong because when it flushes the preferences and you re-enter that screen again the text have been duplicated and placed up on eachother, and some part of the stored text isn´t removeable.
This is how I´ve done:
public final String fieldString = "";
public final String areaString = "";
//
final TextField textField = new TextField(prefs.getString(fieldString),textstyle);
textField.setX(250);
textField.setY(800);
textField.setMaxLength(23);
textField.setWidth(textWidth);
textField.setHeight(textHeight);
stageText.addActor(textField);
I flush the prefs as the user clicks the backbutton to the mainscreen.
btnArrow.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(0);
//Saves the entered text.
prefs.putString(fieldString, textField.getText());
prefs.putString(areaString, textArea.getText());
prefs.flush();
}
});
Without seeing the navigation code, from your description it sounds like multiple text fields are getting added on top of each other each time you return to the screen.

checking for empty values in textfield and spinner

Button btnSearch = (Button) pnlView.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {#
Override
public void onClick(View v) {
results.clear();
if (txtPatientID.getText().equals("") && txtFirstName.getText().equals("")
&& txtLastName.getText().equals("") && txtDOB.getText().equals("")
&& cboGender.getSelectedItem().equals("")) {
Toast.makeText(getActivity(),
"No criteria added", Toast.LENGTH_LONG).show();
return;
}
new Async().execute();
}
});
I have four text field and a spinner, when the user clicks search button, i need to display a message saying no criteria added. Its working, but i am not sure whether i am following the right approach in checking for empty value in textfield and spinner.
You should check the length instead of comparing with empty value. Because it will fail in one or more cases. you should check like this
if(txtPatientID.getText().toString().trim().length()==0)
trim() method will remove one or more spaces in your text
This seems okay. But you can trim() the getText() results to handle empty space characters.

How to check value entered in the Second EditText is Smaller or not in Android

I have two Edit Text fields in my application,if i enter 50 in first edit Text then in the second edit text i want to give permission to enter values less than 50 when the user enters a value in the second edit text, i want to give alert to the user if it is bigger than first edit text value.
How can i do this,means i need to check and show the alert when he enter the values i don't want to show the alert when we press any other fields...
For example:in field 1 if user enter 35
in field2 he has enter value less than 35 only...
initialize both edittext's, lets called them myEditText1 and myEditText2
in myEditText2 set a listener, there should be some kind of listener for when the text changes. Look for something like setOnTextChangedListener, here is how it would be used (this is pseudocode)
myEditText2.setOnTextChangedListener(new OnTextChangedListener(){
#Override
public void OnTextChanged(View v)
{
int i = Integer.valueOf(myEditText1.getText().toString());
int j = Integer.valueOf(myEditText2.getText().toString());
if(j >= i)
{
myEditText2.setText(""); //this automatically sets the editText2 field back to empty
}
}
});
this will only allow the user to enter numerical values lower than the value in the first edit text.
But you need to make sure that the inputType: of both of your editText fields is set to numeric.
You can compare numbers onFocuseChange of second edit text.
editText2.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
// here place code for comparison and calling alert
}
}
});

Categories

Resources