required field validation on android - android

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

Related

How to check if EditText still has error message on clicking submit button

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.

How to retrieve the text entered in an EditText Field from the Listener?

My first android project! I have a Login Activity. Within it there are two EditText fields, "username" and "password". I've made a listener for when the user has completed the field, namely an onEditorActionListener. My motivation behind this is that I need the username and password fields as a String Variable to send to Volley and onto an API. The password Listener is nearly identical to the code below.
final EditText loginEditText_User = (EditText) findViewById(R.id.login_user);
loginEditText_User.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT){
// TODO Retrieve Username here
Log.d("[userName]", loginEditText_User.getText().toString());
loginEditText_Pass.requestFocus();
}
return true;
}
});
I've tried simply declaring a variable String userName before the listener and setting the value within the block via userName = loginEditText_User.getText().toString();, but this doesn't work because "the variable 'userName' is accessed from within inner class, needs to be declared final". However, but when declared final, I obviously "cannot assign value to final variable".
I've seen related questions such as this and this, but they're not quite the same thing.
Thanks in advance!
You can just define you userName variable as a field of your activity, and then you will have an access to it in your scope:
private String userName;
I just answered someone else's question about the same thing here: Edit text first time to input a letter validation
If you use a TextWatcher interface instead, you can get the updated text every time the user adds or removes a letter. Now, If you don't care about the current text until they hit your login button, then you could just do something like. editText.getText() and that returns the current text. The nice thing about the TextWatcher, is you can enable/disable the login button with valid/invalid text. And, it gets you around that whole 'final' issue you mentioned.
This is a known issue every programmer has to deal with a couple of times, there are some options to work around this problem :
1)Use a set function to change the value of the variable, functions can be called anywhere within an inner class.
2)Assign a new variable, which you initialized inside the inner class, with the value you want and use the new variable

EditText methods in java android

Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});

Disabling Textboxes If user enters incorrect password 3 times

Im trying to create a login authentication and when the user enters incorrect password for 3 times.
The texboxes of username and password will be disabled and I'm comparing the user input to my stored username and password in my SQL database to validate the username and password of the user, How can i do this?
you should use a variable that keeps count of attempts. then, when incorrectGuesses equals 3:
editText.setFocusable(false)
or
editText.setEditable(false) // Note that this is deprecated but replacement (inputType) has bug
To set a EditText read only :
Store a reference to your text box (EditText) from your create method and call .setEditable(false) to deactivate them when needed. Maybe you will also call a setText("") on it for clearing the value entered by the user.
There is a relavent issue raised with Google affecting older versions of Android and some random newer devices.
an effecient, reusable, working fix is as follows (assuming this is inside an Activity):
private Static OnClickListener listener;
#Override
public void onCreate(...){
...
listener = new OnClickListener(){ ... };
}
public void disableEditText(EditText t){
t.setOnClickListener(null);
}
public void enableEditText(EditText t){
t.setOnClickListener(listener);
}
issue report for setenabled(false) not working:
http://code.google.com/p/android/issues/detail?id=2771
You should store the number of attempts on SharedPreferences. Once this value reaches 3, just do
editText.setEnabled(false);
You should also check the value of this number in your onCreate or onResume, just so when the user kills the app and restarts it, the EditText will still be disabled.
On another note, you may want to look forward on whether it's a better idea to just disable the OK/Login button (I assume you have that one too) instead of the EditText.

How can I react when an EditText is being focused?

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.

Categories

Resources