0

noobieDeveloper

I have a EditText and TextView as shown

<LinearLayout
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editLength"
            android:hint="LENGTH"
            android:layout_gravity="center_horizontal|center_vertical"
            android:background="@android:color/transparent"
            android:textSize="40dp"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40dp"
            android:visibility="invisible"
            android:textColor="#ffffff"
            android:text="min"/>
    </LinearLayout>

what i want to achieve is when i edit the 'editLength' control i want the TextView to be visible.

I don't want it to do with the glue code(Activity.java). I suppose it can be done through binding of the visibility property of the textbox to the text of 'EditText'.

Can any one guide me how to achieve this?

Anil P Babu
  • 1,235
  • 3
  • 26
  • 47

1 Answers1

1

You should use TextWatcher . It is used to keep watch on the EditText content while user inputs the data. It allows you to keep track on each character when entered on EditText.

Example

         EditText inputObj = (EditText)findViewById(R.id.your_id);
         TextView   outputObj = (TextView)findViewById(R.id.your_id2);
         inputObj .addTextChangedListener(watch);

        }
 // End Oncreate

       TextWatcher watch = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int a, int b, int c) {
            // TODO Auto-generated method stub

            // Add code here //VISIBLE//GONE
        }};

Read How to check if an EditText was changed or not

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • but this is via code. I am looking to bind the visibility property in the xml file itself. (I am coming from WPF background) – Anil P Babu Jan 20 '17 at 09:11