Si8
Si8

Reputation: 9225

EditText onChangeListener function Android

I have the following code:

nameOfInf.setOnFocusChangeListener(new OnFocusChangeListener() {
    if (strTollAmount.length() > 0) {
        nameOfInf.setBackgroundColor(getResources().getColor(android.R.color.white));
        nameOfInf.setTextColor(getResources().getColor(android.R.color.black));
    }
});
tollAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
    if (strInfName.length() > 0) {
        tollAmount.setBackgroundColor(getResources().getColor(android.R.color.white));
        tollAmount.setTextColor(getResources().getColor(android.R.color.black));
    }
});

The function checks to see if the value on the textbox is anything other than empty or null. So if the user enters something in the text box the background and foreground color should change. But that's not happening. Any idea how to edit it?

Upvotes: 3

Views: 11588

Answers (4)

Sadegh
Sadegh

Reputation: 2669

You can your TextWatcher, here is an example:

    final EditText et = new EditText(getApplicationContext());

TextWatcher tw = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (s.length() > 0) {
        et.setBackgroundColor(Color.WHITE);
    } else {
        et.setBackgroundColor(Color.GRAY);
    }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    }
};

et.addTextChangedListener(tw);
setContentView(et);

Upvotes: 1

user1140237
user1140237

Reputation: 5045

txt = (EditText) findViewById(R.id.txtChange);
    txt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (TextUtils.isEmpty(s.toString().trim())) {
                txt.setBackgroundColor(Color.RED);
            } else
                txt.setBackgroundColor(Color.GREEN);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Upvotes: 4

TronicZomB
TronicZomB

Reputation: 8747

I think what you are looking for is the TextWatcher (though the onFocusChanged might work, but this is another approach). Here is sample code:

TextWatcher watcher= new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            if (TextBox1.getText().toString().equals("")) {
                 TextBox1.setBackgroundColor(getResources().getColor(android.R.color.white));
                 TextBox1.setTextColor(getResources().getColor(android.R.color.black));
            }

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              //Do something or nothing.                
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Do something or nothing
        }
    };

    TextBox1.addTextChangedListener(watcher);

Also you will want to do String comparisons using .equals().

Upvotes: 9

Simon Dorociak
Simon Dorociak

Reputation: 33495

Your approach is i think absolutely wrong (it compiles?). You need to test content length of field. I don't know exactly what you are trying to do in your code.

Here are two possible approaches:

if (TextBox1.getText().toString().length() > 0) {
   // is not empty
}
else {
   // is empty
}

or

if (!TextUtils.isEmpty(TextBox1.getText().toString())) {
   // is not empty
}
else {
   // is empty
}

Upvotes: 0

Related Questions