Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 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; |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.text.TextUtils; |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 23 | import android.view.KeyEvent; |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 24 | import android.widget.EditText; |
| 25 | |
| 26 | /** |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 27 | * A custom text editor that helps automatically dismiss the activity along with the soft |
| 28 | * keyboard. |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class SearchEditText extends EditText { |
| 31 | |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 32 | private boolean mMagnifyingGlassShown = true; |
| 33 | private Drawable mMagnifyingGlass; |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 34 | |
| 35 | public SearchEditText(Context context, AttributeSet attrs) { |
| 36 | super(context, attrs); |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 37 | mMagnifyingGlass = getCompoundDrawables()[2]; |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 41 | * Conditionally shows a magnifying glass icon on the right side of the text field |
| 42 | * when the text it empty. |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 43 | */ |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 44 | @Override |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 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; |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 55 | } |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 56 | return super.onPreDraw(); |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 60 | * Forwards the onKeyPreIme call to the view's activity. |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 61 | */ |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame^] | 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); |
Dmitri Plotnikov | db3d143 | 2010-02-09 17:45:47 -0800 | [diff] [blame] | 68 | } |
| 69 | } |