| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.TypedArray; |
| 8 import android.preference.Preference; | 9 import android.preference.Preference; |
| 9 import android.util.AttributeSet; | 10 import android.util.AttributeSet; |
| 10 import android.view.View; | 11 import android.view.View; |
| 11 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
| 12 import android.widget.AdapterView; | 13 import android.widget.AdapterView; |
| 13 import android.widget.ArrayAdapter; | 14 import android.widget.ArrayAdapter; |
| 14 import android.widget.Spinner; | 15 import android.widget.Spinner; |
| 15 import android.widget.TextView; | 16 import android.widget.TextView; |
| 16 | 17 |
| 17 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * A preference that takes value from a specified list of objects, presented as
a dropdown. | 21 * A preference that takes value from a specified list of objects, presented as
a dropdown. |
| 21 */ | 22 */ |
| 22 public class SpinnerPreference extends Preference { | 23 public class SpinnerPreference extends Preference { |
| 23 private Spinner mSpinner; | 24 private Spinner mSpinner; |
| 24 private ArrayAdapter<Object> mAdapter; | 25 private ArrayAdapter<Object> mAdapter; |
| 25 private int mSelectedIndex; | 26 private int mSelectedIndex; |
| 26 private View mView; | 27 private View mView; |
| 27 | 28 |
| 28 /** | 29 /** |
| 29 * Constructor for inflating from XML. | 30 * Constructor for inflating from XML. |
| 30 */ | 31 */ |
| 31 public SpinnerPreference(Context context, AttributeSet attrs) { | 32 public SpinnerPreference(Context context, AttributeSet attrs) { |
| 32 super(context, attrs); | 33 super(context, attrs); |
| 33 setLayoutResource(R.layout.preference_spinner); | 34 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Spinner
Preference); |
| 35 boolean single_line = a.getBoolean(R.styleable.SpinnerPreference_singleL
ine, false); |
| 36 if (single_line) { |
| 37 setLayoutResource(R.layout.preference_spinner_single_line); |
| 38 } else { |
| 39 setLayoutResource(R.layout.preference_spinner); |
| 40 } |
| 34 } | 41 } |
| 35 | 42 |
| 36 /** | 43 /** |
| 37 * Provides a list of arbitrary objects to be shown in the spinner. Visually
, each option will | 44 * Provides a list of arbitrary objects to be shown in the spinner. Visually
, each option will |
| 38 * be presented as its toString() text. | 45 * be presented as its toString() text. |
| 39 * @param options The options to be shown in the spinner. | 46 * @param options The options to be shown in the spinner. |
| 40 * @param selectedIndex Index of the initially selected option. | 47 * @param selectedIndex Index of the initially selected option. |
| 41 */ | 48 */ |
| 42 public void setOptions(Object[] options, int selectedIndex) { | 49 public void setOptions(Object[] options, int selectedIndex) { |
| 43 mAdapter = new ArrayAdapter<Object>( | 50 mAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spin
ner_item, options); |
| 44 getContext(), android.R.layout.simple_spinner_item, options); | |
| 45 mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item); | 51 mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdow
n_item); |
| 46 mSelectedIndex = selectedIndex; | 52 mSelectedIndex = selectedIndex; |
| 47 } | 53 } |
| 48 | 54 |
| 49 /** | 55 /** |
| 50 * @return The currently selected option. | 56 * @return The currently selected option. |
| 51 */ | 57 */ |
| 52 public Object getSelectedOption() { | 58 public Object getSelectedOption() { |
| 53 if (mSpinner == null) return null; | 59 if (mSpinner == null) return null; |
| 54 return mSpinner.getSelectedItem(); | 60 return mSpinner.getSelectedItem(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 93 |
| 88 // Screen readers notice the setAdapter() call and announce it. We do no
t want the spinner | 94 // Screen readers notice the setAdapter() call and announce it. We do no
t want the spinner |
| 89 // to be announced every time the view is bound (e.g. when the user scro
lls away from it | 95 // to be announced every time the view is bound (e.g. when the user scro
lls away from it |
| 90 // and then back). Therefore, only update the adapter if it has actually
changed. | 96 // and then back). Therefore, only update the adapter if it has actually
changed. |
| 91 if (mSpinner.getAdapter() != mAdapter) { | 97 if (mSpinner.getAdapter() != mAdapter) { |
| 92 mSpinner.setAdapter(mAdapter); | 98 mSpinner.setAdapter(mAdapter); |
| 93 } | 99 } |
| 94 mSpinner.setSelection(mSelectedIndex); | 100 mSpinner.setSelection(mSelectedIndex); |
| 95 } | 101 } |
| 96 } | 102 } |
| OLD | NEW |