-1

Is there any way to change the visibility of a button to gone?

I have tried the following code.

<TextView
    android:id="@+id/order_amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"                                
    android:textSize="@dimen/text_size_extra_larger"                                   
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center"
    tools:text="Amount" />

<Button
    android:id="@+id/clear_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_weight="1"                               
    android:background="@android:color/transparent"
    android:text="@string/clear"
    android:textAllCaps="true"
    android:textColor="@android:color/white"
    android:textSize="@dimen/text_size_normal"
    android:visibility='@={orderAmount.getText().equals("0.0") ? View.GONE : View.VISIBLE}' />

I want to make the clear_button disappear when the text in the order_amount is "0.0". I want to accomplish this using data binding expressions only.

kush-mish
  • 11
  • 7
  • This [article](https://stackoverflow.com/questions/4127725/how-can-i-remove-a-button-or-make-it-invisible-in-android) might help you find the anwser – NorSer Apr 23 '19 at 14:19
  • Your TextView *text* must be binded to your model (e.g. to a string called orderAmountValue), so the changes can propagate from one view to the other. Then, your Button can use this orderAmountValue property to execute your check. Without the binding Data Binding cannot detect the changes and notify other views. – Xavier Rubio Jansana Apr 23 '19 at 14:27

1 Answers1

-1

In Kotlin:

clear_button.visibility = View.GONE //View.INVISIBLE

In Java:

clear_button.setVisibility(View.GONE); 

Option 1: You need to call this method from wherever the TextView text is being set dynamically, so for instance:

//... something something something
order_amount.text = amount
if(amount == "0.0") clear_button.visibility = View.GONE

Option 2: Add a TextWatcher to your TextView programmatically, such as this (Kotlin):

order_amount.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(text: Editable?) {
            if(text?.toString() == "0.0") clear_button.visibility = View.GONE
            else clear_button.visibility = View.VISIBLE
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }
    })

In Java:

order_amount.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

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

        @Override
        public void afterTextChanged(Editable s) {
              //change visibility here
        }
    });
Moh Kh
  • 202
  • 3
  • 8