2 * Copyright (C) 2014 The Android Open Source Project
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.settings;
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.preference.Preference;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.AdapterView;
26 import android.widget.AdapterView.OnItemSelectedListener;
27 import android.widget.ArrayAdapter;
28 import android.widget.Spinner;
30 import java.util.ArrayList;
32 public class DropDownPreference extends Preference {
33 private final Context mContext;
34 private final ArrayAdapter<String> mAdapter;
35 private final Spinner mSpinner;
36 private final ArrayList<Object> mValues = new ArrayList<Object>();
38 private Callback mCallback;
39 private int mSelectedPosition = -1;
41 public DropDownPreference(Context context) {
45 public DropDownPreference(Context context, AttributeSet attrs) {
46 super(context, attrs);
48 mAdapter = new ArrayAdapter<String>(mContext,
49 android.R.layout.simple_spinner_dropdown_item);
51 mSpinner = new Spinner(mContext);
53 mSpinner.setVisibility(View.INVISIBLE);
54 mSpinner.setAdapter(mAdapter);
55 mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
57 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
58 setSelectedItem(position, true);
62 public void onNothingSelected(AdapterView<?> parent) {
67 setOnPreferenceClickListener(new OnPreferenceClickListener() {
69 public boolean onPreferenceClick(Preference preference) {
70 mSpinner.performClick();
75 // Support XML specification like ListPreferences do.
76 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownPreference);
77 CharSequence[] entries = a.getTextArray(R.styleable.DropDownPreference_android_entries);
78 CharSequence[] values = a.getTextArray(R.styleable.DropDownPreference_android_entryValues);
79 if (entries != null && values != null) {
80 for (int i= 0; i < entries.length; i++) {
81 addItem(entries[i].toString(), values[i]);
86 public void setDropDownWidth(int dimenResId) {
87 mSpinner.setDropDownWidth(mContext.getResources().getDimensionPixelSize(dimenResId));
90 public void setCallback(Callback callback) {
94 public void setSelectedItem(int position) {
95 setSelectedItem(position, false);
98 public void setSelectedItem(int position, boolean fromSpinner) {
99 if (fromSpinner && position == mSelectedPosition) {
102 final Object value = mValues.get(position);
103 if (mCallback != null && !mCallback.onItemSelected(position, value)) {
106 mSpinner.setSelection(position);
107 mSelectedPosition = mSpinner.getSelectedItemPosition();
108 setSummary(mAdapter.getItem(position));
109 final boolean disableDependents = value == null;
110 notifyDependencyChange(disableDependents);
113 public void setSelectedValue(Object value) {
114 final int i = mValues.indexOf(value);
120 public void addItem(int captionResid, Object value) {
121 addItem(mContext.getResources().getString(captionResid), value);
124 public void addItem(String caption, Object value) {
125 mAdapter.add(caption);
129 public int getItemCount() {
130 return mAdapter.getCount();
133 public void clearItems(){
139 protected void onBindView(View view) {
140 super.onBindView(view);
141 if (view.equals(mSpinner.getParent())) return;
142 if (mSpinner.getParent() != null) {
143 ((ViewGroup)mSpinner.getParent()).removeView(mSpinner);
145 final ViewGroup vg = (ViewGroup)view;
146 vg.addView(mSpinner, 0);
147 final ViewGroup.LayoutParams lp = mSpinner.getLayoutParams();
149 mSpinner.setLayoutParams(lp);
152 public interface Callback {
153 boolean onItemSelected(int pos, Object value);