3

I want to check if the text in some EditText is changed, after user clicks some Button. But View#isDirty seems not to return the correct state of the EditText if called inside onClick. For instance, I wrote something like this:

public class MainActivity extends Activity {
    EditText editText;
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.f);
        editText = (EditText) findViewById(R.id.e);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
            }
        });
    }
}

before i make any change to the editText, it outputs is clean, as expected. But the same is clean is printed even after I write something in editText.
When will isDirty be called? And is it the correct way to do this at all?

Update:

I also want to check if some Switch and Spinner values are changed. Is isDirty() the correct way to do this?

Beleg
  • 149
  • 1
  • 6
  • Hi, did you find an optimal solution for this? I am currently facing the same issue in my app. – Abdul Mateen Nov 12 '19 at 08:37
  • 1
    @AbdulMateen Hi. It's been a long time, but I guess I ended up extending my own form input class that would hold its state (text, checked, item... depending on input type) and checked if it has changed before submitting the form. This way I could also do some validating on input. – Beleg Nov 12 '19 at 12:32
  • 1
    @AbdulMateen The point was, if I remember correctly, isDirty() does not do what I thought it does. Anyways, I've been away from Android since then, maybe there is an internal mechanism for this in new SDK versions. – Beleg Nov 12 '19 at 12:39
  • 1
    The SDK still doesn't have a mechanism for it. Writing my own custom solution :/ – Abdul Mateen Nov 14 '19 at 13:54

2 Answers2

0

I would suggest to use: https://stackoverflow.com/a/9459848/5684335

The comment from Okas is a good explanation why.

Community
  • 1
  • 1
Yonjuni
  • 178
  • 2
  • 12
  • But isDirty returns true after changing text of an EditText, or toggling a Switch, if it's not inside onClick. – Beleg Dec 26 '16 at 14:44
  • I don't know whether this will be the suggestion then, but if your editText is global, you could write another function that will just be called by the onClick-method. – Yonjuni Dec 26 '16 at 14:49
  • But I personally would prefer the `onTextChanged()` – Yonjuni Dec 26 '16 at 14:50
  • Sorry I didn't mentioned in the question, but in fact I have many View's that I have to check, And not all of them are TextViews. So I thought I can use `isDirty()` for either of them independently of what "dirty" means for them. Alternatively, I can make a map of flags that are set in `onTextChanged()` or `OnCheckedChanged()` for Switches, but I thought of this as a more compact way. – Beleg Dec 26 '16 at 14:59
0

By the time you click your button edittext is no longer dirty - text is already updated and view redrawn. Maybe if you change your onclick handler you will understand better what is going on.

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editText = (EditText) findViewById(R.id.e);
            System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
        }
    });

isDirty will return true only as long as view has not been redrawn. This happens quite quickly and basically you do not have (and dont need) any control over this. I think you need to use some other methods to achieve what you want.

Okas
  • 2,664
  • 1
  • 19
  • 26
  • I see, So that's why when I called `isDirty()` inside a Switch's `OnCheckedChanged()` it returned true. That lead me to this fault. – Beleg Dec 26 '16 at 15:14