1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.contacts; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.text.TextUtils; 22 import android.util.AttributeSet; 23 import android.view.KeyEvent; 24 import android.widget.EditText; 25 26 /** 27 * A custom text editor that helps automatically dismiss the activity along with the soft 28 * keyboard. 29 */ 30 public class SearchEditText extends EditText { 31 32 private boolean mMagnifyingGlassShown = true; 33 private Drawable mMagnifyingGlass; 34 35 public SearchEditText(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 mMagnifyingGlass = getCompoundDrawables()[2]; 38 } 39 40 /** 41 * Conditionally shows a magnifying glass icon on the right side of the text field 42 * when the text it empty. 43 */ 44 @Override 45 public boolean onPreDraw() { 46 boolean emptyText = TextUtils.isEmpty(getText()); 47 if (mMagnifyingGlassShown != emptyText) { 48 mMagnifyingGlassShown = emptyText; 49 if (mMagnifyingGlassShown) { 50 setCompoundDrawables(null, null, mMagnifyingGlass, null); 51 } else { 52 setCompoundDrawables(null, null, null, null); 53 } 54 return false; 55 } 56 return super.onPreDraw(); 57 } 58 59 /** 60 * Forwards the onKeyPreIme call to the view's activity. 61 */ 62 @Override 63 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 64 if (((ContactsListActivity)getContext()).onKeyPreIme(keyCode, event)) { 65 return true; 66 } 67 return super.onKeyPreIme(keyCode, event); 68 } 69 } 70