Check edittext change after setting text from API response - android

I need a small help in checking the text in an edittext, so in start of activity i get data from API and set in edittext and i want to check if user made and changes to that edittext, I tried using addTextChangedListener function but this made my Boolean variable true when i set data to editttext from API response.
So is there any other way to check if user did any changes to edittext. and also i have a form in my activity with many edittext, I can compare API response with edittext but it will be very lengthy. If there is no other option ill have to go with that.
Thanks.
I tried finding this question on stackoverflow but didnt find the solution.

Declare a boolean variable in your Activity: Boolean fromApi = false;.
Then before you set the text to the EditText: fromApi = true;
In your textwatcher add at the beginning:
if (fromApi) {
fromApi = false;
return;
}

Related

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

Update text watcher based on other events

In one of my screen i am using text watcher to check value against a range and for invalid range color of text is being changed.
Now there is one more requirement.
Along with EditText i need to use CheckBox to set completely different range.
The problem i am facing is that changing the checkbox value requires to call afterTextChanged in which i have put all validations for both set of ranges.
So basically my requirement is to update textWatcher anyhow so that afterTextChanged get called after i change value of checkbox.
I get a strange feeling that i am forgetting something very simple here,if so please let me know.
Thanks in advance.
Perhaps move your code from the callback in the textWatcher and put it into another function ie
private void checkRange() {
// do checks here
String color = text.getEditableText().toString();
}
and call this method from your textWatcher and from the listener on the checkbox.

required field validation on 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()

Editable textview on click

I have a screen with textviews now i want to make this editable on click of that
i tried one solution using edittext making it as transparent background but initially it will show cursor and the click is not recognizing properly,if i set focusbaleintouchmode to false in xml it is not getting focus.but some how the click is not working properly as expected.first is this correct approach?
expected result is textview should be there once user clicks on it it should be editable once user clicks outside it it should be not editable. any sample code will helps me a lot.sorry for my english
Thanks in advance
finally i got one solution using below code
in xml edit text i gave foucasbletouchmode to false which makes click works properly after that with in onclick
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
to lose focus
et.setFocusable(false);
et.setClickable(true);
et.clearFocus();
You can use the below code :
private makeEditable(boolean isEditable,EditText et){
if(isEditable){
et.setBackgroundDrawable("Give the textbox background here");//You can store it in some variable and use it over here while making non editable.
et.setFocusable(true);
et.setEnabled(true);
et.setClickable(true);
et.setFocusableInTouchMode(true);
et.setKeyListener("Set edit text key listener here"); //You can store it in some variable and use it over here while making non editable.
}else{
et.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
et.setFocusable(false);
et.setClickable(false);
et.setFocusableInTouchMode(false);
et.setEnabled(false);
et.setKeyListener(null);
}
}

Android: How to enable my button back if EditText is not empty?

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.

Categories

Resources