FileDocCategorySizeDatePackage
ApplicationsStackLayout.javaAPI DocAndroid 1.5 Example11118Tue Aug 09 22:58:02 BST 2011com.example.android.home

ApplicationsStackLayout

public class ApplicationsStackLayout extends android.view.ViewGroup implements View.OnClickListener
The ApplicationsStackLayout is a specialized layout used for the purpose of the home screen only. This layout stacks various icons in three distinct areas: the recents, the favorites (or faves) and the button. This layout supports two different orientations: vertical and horizontal. When horizontal, the areas are laid out this way: [RECENTS][FAVES][BUTTON] When vertical, the layout is the following: [RECENTS] [FAVES] [BUTTON] The layout operates from the "bottom up" (or from right to left.) This means that the button area will first be laid out, then the faves area, then the recents. When there are too many favorites, the recents area is not displayed. The following attributes can be set in XML: orientation: horizontal or vertical marginLeft: the left margin of each element in the stack marginTop: the top margin of each element in the stack marginRight: the right margin of each element in the stack marginBottom: the bottom margin of each element in the stack

Fields Summary
public static final int
HORIZONTAL
public static final int
VERTICAL
private android.view.View
mButton
private android.view.LayoutInflater
mInflater
private int
mFavoritesEnd
private int
mFavoritesStart
private List
mFavorites
private List
mRecents
private int
mOrientation
private int
mMarginLeft
private int
mMarginTop
private int
mMarginRight
private int
mMarginBottom
private android.graphics.Rect
mDrawRect
private android.graphics.drawable.Drawable
mBackground
private int
mIconSize
Constructors Summary
public ApplicationsStackLayout(android.content.Context context)


       
        super(context);
        initLayout();
    
public ApplicationsStackLayout(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);

        TypedArray a =
                context.obtainStyledAttributes(attrs, R.styleable.ApplicationsStackLayout);

        mOrientation = a.getInt(R.styleable.ApplicationsStackLayout_stackOrientation, VERTICAL);

        mMarginLeft = a.getDimensionPixelSize(R.styleable.ApplicationsStackLayout_marginLeft, 0);
        mMarginTop = a.getDimensionPixelSize(R.styleable.ApplicationsStackLayout_marginTop, 0);
        mMarginRight = a.getDimensionPixelSize(R.styleable.ApplicationsStackLayout_marginRight, 0);
        mMarginBottom = a.getDimensionPixelSize(R.styleable.ApplicationsStackLayout_marginBottom, 0);

        a.recycle();

        mIconSize = 42; //(int) getResources().getDimension(android.R.dimen.app_icon_size);

        initLayout();
    
Methods Summary
private android.view.ViewcreateApplicationIcon(android.view.LayoutInflater inflater, android.view.ViewGroup group, ApplicationInfo info)


        TextView textView = (TextView) inflater.inflate(R.layout.favorite, group, false);

        info.icon.setBounds(0, 0, mIconSize, mIconSize);
        textView.setCompoundDrawables(null, info.icon, null, null);
        textView.setText(info.title);

        textView.setTag(info.intent);
        textView.setOnClickListener(this);

        return textView;
    
public intgetOrientation()
Return the current orientation, either VERTICAL (default) or HORIZONTAL.

return
the stack orientation

        return mOrientation;
    
private voidinitLayout()

        mInflater = LayoutInflater.from(getContext());
        mButton = mInflater.inflate(R.layout.all_applications_button, this, false);
        addView(mButton);

        mBackground = getBackground();
        setBackgroundDrawable(null);
        setWillNotDraw(false);
    
private voidlayoutHorizontal()

        int childLeft = getWidth();
        int childTop = 0;

        int childWidth = mButton.getMeasuredWidth();
        int childHeight = mButton.getMeasuredHeight();

        childLeft -= childWidth;
        mButton.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        childLeft -= mMarginLeft;
        mFavoritesEnd = childLeft - mMarginRight;

        int oldChildLeft = childLeft;
        childLeft = stackApplications(mFavorites, childLeft, childTop);
        if (childLeft != oldChildLeft) {
            mFavoritesStart = childLeft + mMarginLeft;
        } else {
            mFavoritesStart = -1;
        }

        stackApplications(mRecents, childLeft, childTop);
    
private voidlayoutVertical()

        int childLeft = 0;
        int childTop = getHeight();

        int childWidth = mButton.getMeasuredWidth();
        int childHeight = mButton.getMeasuredHeight();

        childTop -= childHeight + mMarginBottom;
        mButton.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        childTop -= mMarginTop;
        mFavoritesEnd = childTop - mMarginBottom;

        int oldChildTop = childTop;
        childTop = stackApplications(mFavorites, childLeft, childTop);
        if (childTop != oldChildTop) {
            mFavoritesStart = childTop + mMarginTop;
        } else {
            mFavoritesStart = -1;
        }

        stackApplications(mRecents, childLeft, childTop);
    
public voidonClick(android.view.View v)

        getContext().startActivity((Intent) v.getTag());
    
protected voidonDraw(android.graphics.Canvas canvas)

        final Drawable background = mBackground;

        final int right = getWidth();
        final int bottom = getHeight();

        // Draw behind recents
        if (mOrientation == VERTICAL) {
            mDrawRect.set(0, 0, right, mFavoritesStart);
        } else {
            mDrawRect.set(0, 0, mFavoritesStart, bottom);
        }
        background.setBounds(mDrawRect);
        background.draw(canvas);

        // Draw behind favorites
        if (mFavoritesStart > -1) {
            if (mOrientation == VERTICAL) {
                mDrawRect.set(0, mFavoritesStart, right, mFavoritesEnd);
            } else {
                mDrawRect.set(mFavoritesStart, 0, mFavoritesEnd, bottom);
            }
            background.setBounds(mDrawRect);
            background.draw(canvas);
        }

        super.onDraw(canvas);
    
protected voidonLayout(boolean changed, int l, int t, int r, int b)

        removeAllApplications();

        LayoutParams layoutParams = mButton.getLayoutParams();
        final int widthSpec = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY);
        final int heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
        mButton.measure(widthSpec, heightSpec);

        if (mOrientation == VERTICAL) {
            layoutVertical();
        } else {
            layoutHorizontal();
        }
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("ApplicationsStackLayout can only be used with "
                    + "measure spec mode=EXACTLY");
        }

        setMeasuredDimension(widthSize, heightSize);
    
private voidremoveAllApplications()

        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--) {
            final View view = getChildAt(i);
            if (view != mButton) {
                removeViewAt(i);
            }
        }
    
public voidsetFavorites(java.util.List applications)
Sets the list of favorites.

param
applications the applications to put in the favorites area

        mFavorites = applications;
        requestLayout();
    
public voidsetRecents(java.util.List applications)
Sets the list of recents.

param
applications the applications to put in the recents area

        mRecents = applications;
        requestLayout();
    
private intstackApplications(java.util.List applications, int childLeft, int childTop)

        LayoutParams layoutParams;
        int widthSpec;
        int heightSpec;
        int childWidth;
        int childHeight;

        final boolean isVertical = mOrientation == VERTICAL;

        final int count = applications.size();
        for (int i = count - 1; i >= 0; i--) {
            final ApplicationInfo info = applications.get(i);
            final View view = createApplicationIcon(mInflater, this, info);

            layoutParams = view.getLayoutParams();
            widthSpec = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY);
            heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
            view.measure(widthSpec, heightSpec);

            childWidth = view.getMeasuredWidth();
            childHeight = view.getMeasuredHeight();

            if (isVertical) {
                childTop -= childHeight + mMarginBottom;

                if (childTop < 0) {
                    childTop += childHeight + mMarginBottom;
                    break;
                }
            } else {
                childLeft -= childWidth + mMarginRight;

                if (childLeft < 0) {
                    childLeft += childWidth + mMarginRight;
                    break;
                }
            }

            addViewInLayout(view, -1, layoutParams);

            view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);

            if (isVertical) {
                childTop -= mMarginTop;
            } else {
                childLeft -= mMarginLeft;
            }
        }

        return isVertical ? childTop : childLeft;