Bug 1043027 - Only show clear button when there is text in the search bar. r=eedens
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Tue, 19 Aug 2014 12:25:38 -0700
changeset 200323 89304862b06ee5f862e3a81d03b1f139f923c102
parent 200322 c0b30ee8dca44b23f2f28ccb7a0d92214c1a08cf
child 200324 f24fea3d1ab1dc05ae56047b0c2316e370f4c861
push id8281
push usermleibovic@mozilla.com
push dateTue, 19 Aug 2014 19:25:50 +0000
treeherderfx-team@f24fea3d1ab1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerseedens
bugs1043027
milestone34.0a1
Bug 1043027 - Only show clear button when there is text in the search bar. r=eedens
mobile/android/search/java/org/mozilla/search/autocomplete/ClearableEditText.java
--- a/mobile/android/search/java/org/mozilla/search/autocomplete/ClearableEditText.java
+++ b/mobile/android/search/java/org/mozilla/search/autocomplete/ClearableEditText.java
@@ -54,16 +54,18 @@ public class ClearableEditText extends F
             public void onTextChanged(CharSequence s, int start, int before, int count) {
             }
 
             @Override
             public void afterTextChanged(Editable s) {
                 if (listener != null) {
                     listener.onChange(s.toString());
                 }
+
+                updateClearButtonVisibility();
             }
         });
 
         // Attach a listener for the "search" key on the keyboard.
         editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
             @Override
             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                 if (listener != null && actionId == EditorInfo.IME_ACTION_SEARCH) {
@@ -81,17 +83,17 @@ public class ClearableEditText extends F
             public void onFocusChange(View v, boolean hasFocus) {
                 if (listener != null) {
                     listener.onFocusChange(hasFocus);
                 }
             }
         });
 
         clearButton = (ImageButton) findViewById(R.id.clear_button);
-        clearButton.setOnClickListener(new View.OnClickListener(){
+        clearButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 editText.setText("");
             }
         });
 
         inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
     }
@@ -108,33 +110,39 @@ public class ClearableEditText extends F
     }
 
     public void setActive(boolean active) {
         if (this.active == active) {
             return;
         }
         this.active = active;
 
-        clearButton.setVisibility(active ? View.VISIBLE : View.GONE);
+        updateClearButtonVisibility();
 
         editText.setFocusable(active);
         editText.setFocusableInTouchMode(active);
 
         final int leftDrawable = active ? R.drawable.search_icon_active : R.drawable.search_icon_inactive;
         editText.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, 0, 0);
 
         if (active) {
             editText.requestFocus();
             inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
         } else {
             editText.clearFocus();
             inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
         }
     }
 
+    private void updateClearButtonVisibility() {
+        // Only show the clear button when there is text in the input.
+        final boolean visible = active && (editText.getText().length() > 0);
+        clearButton.setVisibility(visible ? View.VISIBLE : View.GONE);
+    }
+
     public void setTextListener(TextListener listener) {
         this.listener = listener;
     }
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent e) {
         // When the view is active, pass touch events to child views.
         // Otherwise, intercept touch events to allow click listeners on the view to