TimeZonePickerView.java revision b064db0d1a7ffcedddd691b35d57e2cdb0268883
1/* 2 * Copyright (C) 2013 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.timezonepicker; 18 19import android.content.Context; 20import android.graphics.drawable.Drawable; 21import android.text.Editable; 22import android.text.Spannable; 23import android.text.SpannableStringBuilder; 24import android.text.TextWatcher; 25import android.text.style.ImageSpan; 26import android.util.AttributeSet; 27import android.view.LayoutInflater; 28import android.view.View; 29import android.widget.AdapterView; 30import android.widget.AdapterView.OnItemClickListener; 31import android.widget.AutoCompleteTextView; 32import android.widget.ImageButton; 33import android.widget.LinearLayout; 34import android.widget.ListView; 35 36public class TimeZonePickerView extends LinearLayout implements TextWatcher, OnItemClickListener { 37 private static final String TAG = "TimeZonePickerView"; 38 39 private Context mContext; 40 private AutoCompleteTextView mAutoCompleteTextView; 41 private TimeZoneFilterTypeAdapter mFilterAdapter; 42 TimeZoneResultAdapter mResultAdapter; 43 44 private ImageButton mClearButton; 45 46 public interface OnTimeZoneSetListener { 47 void onTimeZoneSet(TimeZoneInfo tzi); 48 } 49 50 public TimeZonePickerView(Context context, AttributeSet attrs, 51 String timeZone, long timeMillis, OnTimeZoneSetListener l) { 52 super(context, attrs); 53 mContext = context; 54 LayoutInflater inflater = (LayoutInflater) context.getSystemService( 55 Context.LAYOUT_INFLATER_SERVICE); 56 inflater.inflate(R.layout.timezonepickerview, this, true); 57 58 TimeZoneData tzd = new TimeZoneData(mContext, timeZone, timeMillis); 59 60 mResultAdapter = new TimeZoneResultAdapter(mContext, tzd, l); 61 ListView timeZoneList = (ListView) findViewById(R.id.timezonelist); 62 timeZoneList.setAdapter(mResultAdapter); 63 timeZoneList.setOnItemClickListener(mResultAdapter); 64 65 mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.searchBox); 66 mFilterAdapter = new TimeZoneFilterTypeAdapter(mContext, tzd, mResultAdapter); 67 mAutoCompleteTextView.setAdapter(mFilterAdapter); 68 mAutoCompleteTextView.addTextChangedListener(this); 69 mAutoCompleteTextView.setOnItemClickListener(this); 70 71 updateHint(R.string.hint_time_zone_search, R.drawable.ic_search_holo_light); 72 mClearButton = (ImageButton) findViewById(R.id.clear_search); 73 mClearButton.setOnClickListener(new OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 mAutoCompleteTextView.getEditableText().clear(); 77 } 78 }); 79 } 80 81 private void updateHint(int hintTextId, int imageDrawableId) { 82 String hintText = getResources().getString(hintTextId); 83 Drawable searchIcon = getResources().getDrawable(imageDrawableId); 84 85 SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon 86 ssb.append(hintText); 87 int textSize = (int) (mAutoCompleteTextView.getTextSize() * 1.25); 88 searchIcon.setBounds(0, 0, textSize, textSize); 89 ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 90 mAutoCompleteTextView.setHint(ssb); 91 } 92 93 // Implementation of TextWatcher 94 @Override 95 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 96 } 97 98 // Implementation of TextWatcher 99 @Override 100 public void onTextChanged(CharSequence s, int start, int before, int count) { 101 mFilterAdapter.getFilter().filter(s.toString()); 102 } 103 104 // Implementation of TextWatcher 105 @Override 106 public void afterTextChanged(Editable s) { 107 if (mClearButton != null) { 108 mClearButton.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE); 109 } 110 } 111 112 @Override 113 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 114 // An onClickListener for the view item because I haven't figured out a 115 // way to update the AutoCompleteTextView without causing an infinite loop. 116 mFilterAdapter.onClick(view); 117 } 118} 119