diff options
author | Adam Powell <adamp@google.com> | 2012-03-08 19:43:43 -0800 |
---|---|---|
committer | Adam Powell <adamp@google.com> | 2012-03-08 19:49:39 -0800 |
commit | d9c7be6cc7d18f11731e6d7a1037cc294fde3a4b (patch) | |
tree | 26bbcf805df91ca3787e6978de5551f24c93d923 /core/java/android/widget | |
parent | 787f2699f1f526517c23cf1017ca1a50e88ab8fb (diff) | |
download | frameworks_base-d9c7be6cc7d18f11731e6d7a1037cc294fde3a4b.zip frameworks_base-d9c7be6cc7d18f11731e6d7a1037cc294fde3a4b.tar.gz frameworks_base-d9c7be6cc7d18f11731e6d7a1037cc294fde3a4b.tar.bz2 |
Public API for android.widget.Spinner properties
Bug 6104467
Add properties that can be set/retrieved programmatically to match the
XML attributes available.
Also add resource ID versions of Drawable setters for Switch.
Change-Id: I198cfd9701189ab200c3190f61d18c459b7e4591
Diffstat (limited to 'core/java/android/widget')
-rw-r--r-- | core/java/android/widget/Spinner.java | 172 | ||||
-rw-r--r-- | core/java/android/widget/Switch.java | 23 |
2 files changed, 195 insertions, 0 deletions
diff --git a/core/java/android/widget/Spinner.java b/core/java/android/widget/Spinner.java index 6540613..d571a2b 100644 --- a/core/java/android/widget/Spinner.java +++ b/core/java/android/widget/Spinner.java @@ -26,6 +26,7 @@ import android.database.DataSetObserver; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.util.AttributeSet; +import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -202,6 +203,130 @@ public class Spinner extends AbsSpinner implements OnClickListener { } } + /** + * Set the background drawable for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. + * + * @param background Background drawable + * + * @attr ref android.R.styleable#Spinner_popupBackground + */ + public void setPopupBackgroundDrawable(Drawable background) { + if (!(mPopup instanceof DropdownPopup)) { + Log.e(TAG, "setPopupBackgroundDrawable: incompatible spinner mode; ignoring..."); + return; + } + ((DropdownPopup) mPopup).setBackgroundDrawable(background); + } + + /** + * Set the background drawable for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. + * + * @param background Resource ID of a background drawable + * + * @attr ref android.R.styleable#Spinner_popupBackground + */ + public void setPopupBackgroundResource(int resId) { + setPopupBackgroundDrawable(getContext().getResources().getDrawable(resId)); + } + + /** + * Get the background drawable for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; other modes will return null. + * + * @return background Background drawable + * + * @attr ref android.R.styleable#Spinner_popupBackground + */ + public Drawable getPopupBackground() { + return mPopup.getBackground(); + } + + /** + * Set a vertical offset in pixels for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. + * + * @param pixels Vertical offset in pixels + * + * @attr ref android.R.styleable#Spinner_dropDownVerticalOffset + */ + public void setDropDownVerticalOffset(int pixels) { + mPopup.setVerticalOffset(pixels); + } + + /** + * Get the configured vertical offset in pixels for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; other modes will return 0. + * + * @return Vertical offset in pixels + * + * @attr ref android.R.styleable#Spinner_dropDownVerticalOffset + */ + public int getDropDownVerticalOffset() { + return mPopup.getVerticalOffset(); + } + + /** + * Set a horizontal offset in pixels for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. + * + * @param pixels Horizontal offset in pixels + * + * @attr ref android.R.styleable#Spinner_dropDownHorizontalOffset + */ + public void setDropDownHorizontalOffset(int pixels) { + mPopup.setHorizontalOffset(pixels); + } + + /** + * Get the configured horizontal offset in pixels for the spinner's popup window of choices. + * Only valid in {@link #MODE_DROPDOWN}; other modes will return 0. + * + * @return Horizontal offset in pixels + * + * @attr ref android.R.styleable#Spinner_dropDownHorizontalOffset + */ + public int getDropDownHorizontalOffset() { + return mPopup.getHorizontalOffset(); + } + + /** + * Set the width of the spinner's popup window of choices in pixels. This value + * may also be set to {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} + * to match the width of the Spinner itself, or + * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} to wrap to the measured size + * of contained dropdown list items. + * + * <p>Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes.</p> + * + * @param pixels Width in pixels, WRAP_CONTENT, or MATCH_PARENT + * + * @attr ref android.R.styleable#Spinner_dropDownWidth + */ + public void setDropDownWidth(int pixels) { + if (!(mPopup instanceof DropdownPopup)) { + Log.e(TAG, "Cannot set dropdown width for MODE_DIALOG, ignoring"); + return; + } + mDropDownWidth = pixels; + } + + /** + * Get the configured width of the spinner's popup window of choices in pixels. + * The returned value may also be {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} + * meaning the popup window will match the width of the Spinner itself, or + * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} to wrap to the measured size + * of contained dropdown list items. + * + * @return Width in pixels, WRAP_CONTENT, or MATCH_PARENT + * + * @attr ref android.R.styleable#Spinner_dropDownWidth + */ + public int getDropDownWidth() { + return mDropDownWidth; + } + @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); @@ -231,6 +356,16 @@ public class Spinner extends AbsSpinner implements OnClickListener { } } + /** + * Describes how the selected item view is positioned. The default is determined by the + * current theme. + * + * @return A {@link android.view.Gravity Gravity} value + */ + public int getGravity() { + return mGravity; + } + @Override public void setAdapter(SpinnerAdapter adapter) { super.setAdapter(adapter); @@ -675,6 +810,13 @@ public class Spinner extends AbsSpinner implements OnClickListener { */ public void setPromptText(CharSequence hintText); public CharSequence getHintText(); + + public void setBackgroundDrawable(Drawable bg); + public void setVerticalOffset(int px); + public void setHorizontalOffset(int px); + public Drawable getBackground(); + public int getVerticalOffset(); + public int getHorizontalOffset(); } private class DialogPopup implements SpinnerPopup, DialogInterface.OnClickListener { @@ -719,6 +861,36 @@ public class Spinner extends AbsSpinner implements OnClickListener { } dismiss(); } + + @Override + public void setBackgroundDrawable(Drawable bg) { + Log.e(TAG, "Cannot set popup background for MODE_DIALOG, ignoring"); + } + + @Override + public void setVerticalOffset(int px) { + Log.e(TAG, "Cannot set vertical offset for MODE_DIALOG, ignoring"); + } + + @Override + public void setHorizontalOffset(int px) { + Log.e(TAG, "Cannot set horizontal offset for MODE_DIALOG, ignoring"); + } + + @Override + public Drawable getBackground() { + return null; + } + + @Override + public int getVerticalOffset() { + return 0; + } + + @Override + public int getHorizontalOffset() { + return 0; + } } private class DropdownPopup extends ListPopupWindow implements SpinnerPopup { diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index 23e55e2..507d73a 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -359,6 +359,17 @@ public class Switch extends CompoundButton { } /** + * Set the drawable used for the track that the switch slides within. + * + * @param track Resource ID of a track drawable + * + * @attr ref android.R.styleable#Switch_track + */ + public void setTrackResource(int resId) { + setTrackDrawable(getContext().getResources().getDrawable(resId)); + } + + /** * Get the drawable used for the track that the switch slides within. * * @return Track drawable @@ -383,6 +394,18 @@ public class Switch extends CompoundButton { } /** + * Set the drawable used for the switch "thumb" - the piece that the user + * can physically touch and drag along the track. + * + * @param thumb Resource ID of a thumb drawable + * + * @attr ref android.R.styleable#Switch_thumb + */ + public void setThumbResource(int resId) { + setThumbDrawable(getContext().getResources().getDrawable(resId)); + } + + /** * Get the drawable used for the switch "thumb" - the piece that the user * can physically touch and drag along the track. * |