Dinesh Singh
Dinesh Singh

Reputation: 837

Unable to update View in addTextChangedListener()

I am trying to edit the background of a Button depending on the length of text in EditText. If the length of text is 0 then the background should be translucent else it should be opaque. However, I am not able to update the Button's background dynamically. If I tap somewhere else on the screen then the button's background is updated, but not in real time.

Here is the code I am using.

emailEditText.addTextChangedListener(new TextWatcher(){

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

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if(s.length()!=0){
                submitEmail.getBackground().setAlpha(255);
            }
            else{
                submitEmail.getBackground().setAlpha(45);
            }


        }

    });

Any help will be highly appreciated.

Upvotes: 1

Views: 273

Answers (1)

the-ginger-geek
the-ginger-geek

Reputation: 7081

Please do this call after setting the value

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {

        if(s.length()!=0){
            submitEmail.getBackground().setAlpha(255);
        }
        else{
            submitEmail.getBackground().setAlpha(45);
        }

        submitEmail.invalidate();

    }

Upvotes: 1

Related Questions