1

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.

Dinesh Singh
  • 727
  • 1
  • 10
  • 22

1 Answers1

1

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

    }
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45