blob: 7683f239f8bbbbd7cfbabceb10bee14a64868fb4 [file] [log] [blame]
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -08001/*
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
17package com.android.contacts;
18
19import android.content.Context;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080020import android.graphics.drawable.Drawable;
21import android.text.TextUtils;
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080022import android.util.AttributeSet;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080023import android.view.KeyEvent;
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080024import android.widget.EditText;
25
26/**
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080027 * A custom text editor that helps automatically dismiss the activity along with the soft
28 * keyboard.
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080029 */
30public class SearchEditText extends EditText {
31
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080032 private boolean mMagnifyingGlassShown = true;
33 private Drawable mMagnifyingGlass;
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080034
35 public SearchEditText(Context context, AttributeSet attrs) {
36 super(context, attrs);
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080037 mMagnifyingGlass = getCompoundDrawables()[2];
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080038 }
39
40 /**
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080041 * Conditionally shows a magnifying glass icon on the right side of the text field
42 * when the text it empty.
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080043 */
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080044 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080045 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;
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080055 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080056 return super.onPreDraw();
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080057 }
58
59 /**
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080060 * Forwards the onKeyPreIme call to the view's activity.
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080061 */
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080062 @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);
Dmitri Plotnikovdb3d1432010-02-09 17:45:47 -080068 }
69}