FileDocCategorySizeDatePackage
Spinner.javaAPI DocAndroid 1.5 API10798Wed May 06 22:41:56 BST 2009android.widget

Spinner

public class Spinner extends AbsSpinner implements android.content.DialogInterface.OnClickListener
A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the {@link Adapter} associated with this view.
attr
ref android.R.styleable#Spinner_prompt

Fields Summary
private CharSequence
mPrompt
Constructors Summary
public Spinner(android.content.Context context)

        this(context, null);
    
public Spinner(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, com.android.internal.R.attr.spinnerStyle);
    
public Spinner(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.Spinner, defStyle, 0);
        
        mPrompt = a.getString(com.android.internal.R.styleable.Spinner_prompt);

        a.recycle();
    
Methods Summary
public intgetBaseline()

        View child = null;

        if (getChildCount() > 0) {
            child = getChildAt(0);
        } else if (mAdapter != null && mAdapter.getCount() > 0) {
            child = makeAndAddView(0);
            // TODO: We should probably put the child in the recycler
        }

        if (child != null) {
            return child.getTop() + child.getBaseline();
        } else {
            return -1;
        }
    
public java.lang.CharSequencegetPrompt()

return
The prompt to display when the dialog is shown

        return mPrompt;
    
voidlayout(int delta, boolean animate)
Creates and positions all views for this Spinner.

param
delta Change in the selected position. +1 moves selection is moving to the right, so views are scrolling to the left. -1 means selection is moving to the left.

        int childrenLeft = mSpinnerPadding.left;
        int childrenWidth = mRight - mLeft - mSpinnerPadding.left - mSpinnerPadding.right;

        if (mDataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views
        if (mItemCount == 0) {
            resetList();
            return;
        }

        if (mNextSelectedPosition >= 0) {
            setSelectedPositionInt(mNextSelectedPosition);
        }

        recycleAllViews();

        // Clear out old views
        removeAllViewsInLayout();

        // Make selected view and center it
        mFirstPosition = mSelectedPosition;
        View sel = makeAndAddView(mSelectedPosition);
        int width = sel.getMeasuredWidth();
        int selectedOffset = childrenLeft + (childrenWidth / 2) - (width / 2);
        sel.offsetLeftAndRight(selectedOffset);

        // Flush any cached views that did not get reused above
        mRecycler.clear();

        invalidate();

        checkSelectionChanged();

        mDataChanged = false;
        mNeedSync = false;
        setNextSelectedPositionInt(mSelectedPosition);
    
private android.view.ViewmakeAndAddView(int position)
Obtain a view, either by pulling an existing view from the recycler or by getting a new one from the adapter. If we are animating, make sure there is enough information in the view's layout parameters to animate from the old to new positions.

param
position Position in the spinner for the view to obtain
return
A view that has been added to the spinner


        View child;

        if (!mDataChanged) {
            child = mRecycler.get(position);
            if (child != null) {
                // Position the view
                setUpChild(child);

                return child;
            }
        }

        // Nothing found in the recycler -- ask the adapter for a view
        child = mAdapter.getView(position, null, this);

        // Position the view
        setUpChild(child);

        return child;
    
public voidonClick(android.content.DialogInterface dialog, int which)

        setSelection(which);
        dialog.dismiss();
    
protected voidonLayout(boolean changed, int l, int t, int r, int b)

see
android.view.View#onLayout(boolean,int,int,int,int) Creates and positions all views

        super.onLayout(changed, l, t, r, b);
        mInLayout = true;
        layout(0, false);
        mInLayout = false;
    
public booleanperformClick()

        boolean handled = super.performClick();
        
        if (!handled) {
            handled = true;
            Context context = getContext();
            
            final DropDownAdapter adapter = new DropDownAdapter(getAdapter());

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            if (mPrompt != null) {
                builder.setTitle(mPrompt);
            }
            builder.setSingleChoiceItems(adapter, getSelectedItemPosition(), this).show();
        }

        return handled;
    
public voidsetOnItemClickListener(OnItemClickListener l)

A spinner does not support item click events. Calling this method will raise an exception.

param
l this listener will be ignored

        throw new RuntimeException("setOnItemClickListener cannot be used with a spinner.");
    
public voidsetPrompt(java.lang.CharSequence prompt)
Sets the prompt to display when the dialog is shown.

param
prompt the prompt to set

        mPrompt = prompt;
    
public voidsetPromptId(int promptId)
Sets the prompt to display when the dialog is shown.

param
promptId the resource ID of the prompt to display when the dialog is shown

        mPrompt = getContext().getText(promptId);
    
private voidsetUpChild(android.view.View child)
Helper for makeAndAddView to set the position of a view and fill out its layout paramters.

param
child The view to position


        // Respect layout params that are already in the view. Otherwise
        // make some up...
        ViewGroup.LayoutParams lp = child.getLayoutParams();
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        }

        addViewInLayout(child, 0, lp);

        child.setSelected(hasFocus());

        // Get measure specs
        int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
                mSpinnerPadding.top + mSpinnerPadding.bottom, lp.height);
        int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
                mSpinnerPadding.left + mSpinnerPadding.right, lp.width);

        // Measure child
        child.measure(childWidthSpec, childHeightSpec);

        int childLeft;
        int childRight;

        // Position vertically based on gravity setting
        int childTop = mSpinnerPadding.top
                + ((mMeasuredHeight - mSpinnerPadding.bottom - 
                        mSpinnerPadding.top - child.getMeasuredHeight()) / 2);
        int childBottom = childTop + child.getMeasuredHeight();

        int width = child.getMeasuredWidth();
        childLeft = 0;
        childRight = childLeft + width;

        child.layout(childLeft, childTop, childRight, childBottom);