Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display TextView when EditText is not empty

I'm working with an Android application and have encountered the following problem.

Problem: I want a TextView to display some text when two edittext fields are not empty (when there are text written in the edittext fields).

What I have done:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle   
savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);  
...

if(edittextbox1.getText().toString().length() !=0 && edittextbox2.getText().toString().length() !=0) {
    textview.setText("some text")
} 
return rootView;
}

This does not work. The text view never pops up. Any ideas why?

like image 873
John Avatar asked Jul 01 '26 07:07

John


1 Answers

Try this add TextWatcher to both edit text. You can get the text changed for both edit text and then you get the length and show your textView if the condition is true.

TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int st, int b, int c)
        {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int st, int c, int a)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if(edittextbox1.getText().toString().length() != 0 && edittextbox2.getText().toString().length() != 0) {
                 textView.setVisibility(View.VISIBLE);
                 textview.setText("some text");
             }
        }
    };

  edittextbox1.addTextChangedListener(textWatcher);
  edittextbox2.addTextChangedListener(textWatcher);
like image 182
Faysal Ahmed Avatar answered Jul 02 '26 21:07

Faysal Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!