Right now I'm building a simple form and I'm designing it so that if the user hasn't entered the necessary info before clicking the submit button the background turns red. And after if they have entered the correct info the form goes back to the way it was before.
// "if empty then set fields to red" checks
if (firstLastName.getText().toString().equals("")) {
firstLastName.setBackgroundColor(Color.RED);
}
else
firstLastName.setBackgroundColor(Color.WHITE);
}
The problem is that white apparently isn't what it was before, because it looks different. Is there a way to reset the form without deleting the info entered by the user?
If I'm not being clear please let me know and Ill try to elaborate.
How about setting and removing a color filter instead of changing the background color:
if (firstLastName.getText().toString().equals("")) {
// 0xFFFF0000 is red
firstLastName.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.DARKEN);}
else {
//Setting to null removes filter
firstLastName.getBackground().setColorFilter(null);
}
Related
I'm starting with one programatically created AutoCompleteTextView view on my layout. After user typed valid data into the field, I'm creating a new AutoCompleteTextView object right under the first one by the same way, with the same parameters. I'm storing all of the field references in an ArrayList to keep up with the last one, this way I'm making sure that there will be new fields only under the one at the bottom (basically the last element in the reference list) - so it goes on at every field.
I would like to add this feature: when a new field is created, I change the last field's IME options to EditorInfo.IME_ACTION_NEXT and nextFocusForward attribute to the freshly created field - also programmatically. What I want to achieve: right when user presses Enter on the keyboard, last field's focus jumps to the new field. I'm using this code to set new IME options:
ArrayList<AutoCompleteTextView> fields = new ArrayList<>();
//Creating freshlyCreatedField, IT'S STILL NOT IN THE fields LIST!
AutoCompleteTextView currentlySelectedField = fields.get(fields.size() - 1); // last element
currentlySelectedField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
currentlySelectedField.setNextFocusForwardId(freshlyCreatedField.getId());
currentlySelectedField.setSingleLine(true); //ACTV needs it to get IME to work
//Adding freshlyCreatedField to the fields list,
//so next time it will be the currentlySelectedField
But after running this code nothing happens to the currently selected AutoCompleteTextView object. Clearing and requesting focus did not work, and making it work by the reversed way (creating +1 "invisible" (technically GONE) field everytime) would be much more painful.
One more thing: after selecting any other field and re-selecting the one with configuration changes the Enter button works as it should first time! If I could do the same programmatically, it would solve my problem... so, any ideas how to do it? (Of course, I welcome better solutions also... ;) )
After some days, I've started to play with this problem again. Yes, it is a solution, but no, it's not so pretty as I would it to be like...
Anyhow, I created this code:
ArrayList<AutoCompleteTextView> fields = new ArrayList<>();
//Creating freshlyCreatedField, IT'S STILL NOT IN THE fields LIST!
//Last element
AutoCompleteTextView currentlySelectedField = fields.get(fields.size() - 1);
currentlySelectedField.setOnEditorActionListener(
new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_DONE
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
freshlyCreatedField.requestFocus();
handled = true;
}
return handled;
}
});
//Adding freshlyCreatedField to the fields list,
//so next time it will be the currentlySelectedField
I put an EditorActionListener to the field actually under edit where the keyboard listens for one of the IME_ACTION_NEXT (if it's already set) and IME_ACTION_DONE action IDs (it's the default), or for the Enter key (for the case if someone would like to use physical keyboard). Then it jumps to the field below.
It works great, there aren't terrible problems with this approach. However I'm a little bit sad, because all of these possibilities stay invisible. The Done action button switches to Next only by clicking to somewhere else as I wrote it down in my question. So I think my "solution" can work, but it's not the best answer, so feel free to write your thoughts down if you have another idea... :)
I am creating a Sign-Up form.
I have used setError() to set error messages in all EditText s. now I want to check that whether an EditText has error message or not when I click on the submit button.
To check text after it has been written but before clicking register/submit button on your page, you need to use TextWatcher
After setting up error with setError(), you need to check if getError() returns null as well as editText.getText().length() returns 0 then perform action like Toast or anything.
E.g:
onClick(View v){
if(editText.getError() != null || editText.getText().length() == 0){
................(Perform Toast or something for clearing error)
}else{
.... your action to perform on button click
}
You could use the getError() method for the EditText and check if it returns null. Although a better way would be to just set a boolean isValid on errors and check against that
Right approach to achieve an error less submission of your form will be following:
on click of submit check whether entered text is a correct email.
if its not correct show error using setError.
if its correct go ahead and submit your form.
you need not to check explicitly if error is set or not as upon next submit field will be re-verified anyways.
In android how do I create a required field validation? like on a page where use can enter some data into some EditText. I wanted to make sure user enter something before continuing, like if the user forgot to enter some data, the app would not crash. I have done other validation like number only using those input-type provided. but so far I research I only found ways to validate content entered but not whether there is something entered.
So I should put something in the onCreate method like
if(EditText text is !=null){ do the stuff} else {error message}
But if I did that, the moment the app is run there will be error displayed.
And how do I write the "text of EditText" in c# I believe is TextBox Text. But I do not know how to do that in java android. I know setText but do not know how to refer to the content without changing it.
To get text user entered in the EditText you can call getText() method. I recommend you to perform validation after user clicks some button. Validating content of EditText inside onCreate() method is useless.
It's always better to tell the user that they need to put the correct information the earliest possible. Why? Because it allows the user to quickly identify the problem and fix it. That being said, I would recommend checking if its null or whatever you want the textfield to contain as soon as it looses focus.
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//We get the text and then convert it to a string and check
if(v.getText().toString() == ""){
//Do what you want in this area
}
}
});
String str = textview.getText().toString().trim()
I am new to android and programming. I am writing an app for calculating BMI. It works fine but there is a problem. The basic structure is:
Weight: "Edittext"
Height: "Edittext"
Reset Calculate <--- buttons
The app hangs whenever the user presses calculate button without entering a value (Height or weight). thats happening because a mathematical calculation is taking place without any values. how can i prevent the calculation from taking place when there are no values?????
Thankyou in advance
In the onClick function for the calculate button, check to see if the values make sense. If they don't, then don't do anything...
prior to any operation check for null values .
onCkick(View V)
{
if(editWeight.getText() != null or editHeight.getText() != null)
{
//do operation
}
}
I have an EditText that the user can write in, when the app starts there is already a string in the EditText. When the user clicks the EditText it becomes focused and the curser is where the user clicked the EditText text box.
I know that the code for setting the curser to the start is :
editText.setSelection(0);
But I don't know where to put this code, I tried to it in beforeTextChanged but it didn't do the job.
You can do this by setting an putting an OnFocusChangedListener. You'd do something like this:
et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasFocus){
if(hasFocus){
((EditText)view).setSelection(0);
}
}
});
Where et is the text edit you want to set the listener on.
Full-discolsure: haven't tried this code out myself.
While there is probably a way to do this, I'm not entirely sure it's the best user experience, because when the user taps a text box at a specific spot, they really expect the cursor to be there. Imagine for instance if the user sees "abcd" written there and wants to edit that to "abcde", so they figure "I'll just tap at the end and append an 'e'". Imagine the user's frustration when that doesn't work as expected.
If you expect the user to edit the textbox, I'd consider leaving it empty. If you are using the existing text as a hint ("email#example.com"), it's probably a better idea to indicate that in some other way.