'Changing Background based on update of TextView

I am trying to create a simple android app using JAVA(Android Studio). What I am willing to implement is the background color of the app to change based on the content of the TextView (which is updated constantly between two values "Open" and "Closed"). I have seen that onChanged() must be used but I don`t really understand how to Implement the listener for the TextView.

I tried wrote this : measurementValue = (TextView) findViewById(R.id.textbox_main); measurementValue.addTextChangedListener(new TextWatcher() {} based on what I have found online, but can`t really figure out what the next step is. I am a total newbie so please be kind.

Best regards!



Solution 1:[1]

You can listen for text change on those 3 callbacks. It depends on your use case.

@Override
public void onTextChanged(CharSequence textInput, int start, int before, int count) {
    String strInput = textInput.toString();

    if ("open".equalsIgnoreCase(strInput)) {
        // text is "open"
        // change background to desired color
    } else if ("closed".equalsIgnoreCase(strInput)) {
       // text is "closed"
       // change background to desired color
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jbmcle