2 * Copyright (C) 2014 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.i18n.addressinput;
19 import android.app.ProgressDialog;
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.widget.ArrayAdapter;
23 import android.widget.EditText;
24 import android.widget.Spinner;
25 import android.widget.TextView;
27 import com.android.i18n.addressinput.AddressField.WidthType;
30 * Base class for customizing widgets for address input.
33 * Clients can optionally override this class and use
34 * {@link AddressWidget#setUiComponentProvider(AddressWidgetUiComponentProvider)} to set the the
35 * componentProvider field of the address widget, which will be invoked by the widget to create UI
36 * components that provide consistent look-and-feel with other UI components clients might use
37 * alongside the address widget.
39 public class AddressWidgetUiComponentProvider {
40 protected Context context;
41 protected LayoutInflater inflater;
43 public AddressWidgetUiComponentProvider(Context context) {
44 this.context = context;
45 this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
49 * Creates a label, e.g. "State", for an address input field.
51 * @param label the label of the address input field
52 * @param widthType {@link WidthType} of the field
53 * @return a custom {@link TextView} created for the field
55 protected TextView createUiLabel(CharSequence label, WidthType widthType) {
56 TextView textView = (TextView) inflater.inflate(R.layout.address_textview, null, false);
57 textView.setText(label);
62 * Creates a text input view for an address input field.
64 * @param widthType {@link WidthType} of the field
65 * @return a custom {@link EditText} created for the field
67 protected EditText createUiTextField(WidthType widthType) {
68 return (EditText) inflater.inflate(R.layout.address_edittext, null, false);
72 * Creates a {@link Spinner} for a input field that uses UI picker.
74 * @param widthType {@link WidthType} of the field
75 * @return a custom {@link Spinner} created for the field
77 protected Spinner createUiPickerSpinner(WidthType widthType) {
78 return (Spinner) inflater.inflate(R.layout.address_spinner, null, false);
82 * Creates an {@link ArrayAdapter} to work with the custom {@link Spinner} of a input field that
85 * @param widthType {@link WidthType} of the field
86 * @return a custom {@link ArrayAdapter} for the field
88 protected ArrayAdapter<String> createUiPickerAdapter(WidthType widthType) {
89 ArrayAdapter<String> adapter =
90 new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
91 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
95 /** Gets an activity indicator to show that a task is in progress. */
96 protected ProgressDialog getUiActivityIndicatorView() {
97 return new ProgressDialog(context);