Example usage for android.widget LinearLayout HORIZONTAL

List of usage examples for android.widget LinearLayout HORIZONTAL

Introduction

In this page you can find the example usage for android.widget LinearLayout HORIZONTAL.

Prototype

int HORIZONTAL

To view the source code for android.widget LinearLayout HORIZONTAL.

Click Source Link

Usage

From source file:com.mishiranu.dashchan.ui.navigator.NavigatorActivity.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void showThemeDialog() {
    final int checkedItem = Arrays.asList(Preferences.VALUES_THEME).indexOf(Preferences.getTheme());
    Resources resources = getResources();
    float density = ResourceUtils.obtainDensity(resources);
    ScrollView scrollView = new ScrollView(this);
    LinearLayout outer = new LinearLayout(this);
    outer.setOrientation(LinearLayout.VERTICAL);
    int outerPadding = (int) (16f * density);
    outer.setPadding(outerPadding, outerPadding, outerPadding, outerPadding);
    scrollView.addView(outer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.action_change_theme)
            .setView(scrollView).setNegativeButton(android.R.string.cancel, null).create();
    View.OnClickListener listener = v -> {
        int index = (int) v.getTag();
        if (index != checkedItem) {
            Preferences.setTheme(Preferences.VALUES_THEME[index]);
            recreate();//from w  w  w . ja v a 2 s .c o m
        }
        dialog.dismiss();
    };
    int circleSize = (int) (56f * density);
    int itemPadding = (int) (12f * density);
    LinearLayout inner = null;
    for (int i = 0; i < Preferences.ENTRIES_THEME.length; i++) {
        if (i % 3 == 0) {
            inner = new LinearLayout(this);
            inner.setOrientation(LinearLayout.HORIZONTAL);
            outer.addView(inner, LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.CENTER);
        layout.setBackgroundResource(
                ResourceUtils.getResourceId(this, android.R.attr.selectableItemBackground, 0));
        layout.setPadding(0, itemPadding, 0, itemPadding);
        layout.setOnClickListener(listener);
        layout.setTag(i);
        inner.addView(layout, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
        View view = new View(this);
        int colorBackgroundAttr = Preferences.VALUES_THEME_COLORS[i][0];
        int colorPrimaryAttr = Preferences.VALUES_THEME_COLORS[i][1];
        int colorAccentAttr = Preferences.VALUES_THEME_COLORS[i][2];
        Resources.Theme theme = getResources().newTheme();
        theme.applyStyle(Preferences.VALUES_THEME_IDS[i], true);
        TypedArray typedArray = theme
                .obtainStyledAttributes(new int[] { colorBackgroundAttr, colorPrimaryAttr, colorAccentAttr });
        view.setBackground(new ThemeChoiceDrawable(typedArray.getColor(0, 0), typedArray.getColor(1, 0),
                typedArray.getColor(2, 0)));
        typedArray.recycle();
        if (C.API_LOLLIPOP) {
            view.setElevation(6f * density);
        }
        layout.addView(view, circleSize, circleSize);
        TextView textView = new TextView(this, null, android.R.attr.textAppearanceListItem);
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setText(Preferences.ENTRIES_THEME[i]);
        if (C.API_LOLLIPOP) {
            textView.setAllCaps(true);
            textView.setTypeface(GraphicsUtils.TYPEFACE_MEDIUM);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
        } else {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
        }
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        textView.setPadding(0, (int) (8f * density), 0, 0);
        layout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        if (i + 1 == Preferences.ENTRIES_THEME.length && Preferences.ENTRIES_THEME.length % 3 != 0) {
            if (Preferences.ENTRIES_THEME.length % 3 == 1) {
                inner.addView(new View(this), 0,
                        new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
            }
            inner.addView(new View(this),
                    new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
        }
    }
    dialog.show();
}

From source file:com.mishiranu.dashchan.ui.navigator.DrawerForm.java

private View makeView(boolean icon, boolean watcher, boolean closeable, float density) {
    int size = (int) (48f * density);
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.setGravity(Gravity.CENTER_VERTICAL);
    ImageView iconView = null;/*  w  w  w  .  ja  v a 2  s . c om*/
    if (icon) {
        iconView = new ImageView(context);
        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        linearLayout.addView(iconView, (int) (24f * density), size);
    }
    TextView textView = makeCommonTextView(false);
    linearLayout.addView(textView, new LinearLayout.LayoutParams(0, size, 1));
    WatcherView watcherView = null;
    if (watcher) {
        watcherView = new WatcherView(context);
        linearLayout.addView(watcherView, size, size);
    }
    ImageView closeView = null;
    if (!watcher && closeable) {
        closeView = new ImageView(context);
        closeView.setScaleType(ImageView.ScaleType.CENTER);
        closeView.setImageResource(ResourceUtils.getResourceId(context, R.attr.buttonCancel, 0));
        closeView.setBackgroundResource(ResourceUtils.getResourceId(context,
                android.R.attr.borderlessButtonStyle, android.R.attr.background, 0));
        linearLayout.addView(closeView, size, size);
        closeView.setOnClickListener(closeButtonListener);
    }
    ViewHolder holder = new ViewHolder();
    holder.icon = iconView;
    holder.text = textView;
    holder.extra = watcherView != null ? watcherView : closeView;
    linearLayout.setTag(holder);
    int layoutLeftDp = 0;
    int layoutRightDp = 0;
    int textLeftDp;
    int textRightDp;
    if (C.API_LOLLIPOP) {
        textLeftDp = 16;
        textRightDp = 16;
        if (icon) {
            layoutLeftDp = 16;
            textLeftDp = 32;
        }
        if (watcher || closeable) {
            layoutRightDp = 4;
            textRightDp = 8;
        }
    } else {
        textLeftDp = 8;
        textRightDp = 8;
        if (icon) {
            layoutLeftDp = 8;
            textLeftDp = 6;
            textView.setAllCaps(true);
        }
        if (watcher || closeable) {
            layoutRightDp = 0;
            textRightDp = 0;
        }
    }
    linearLayout.setPadding((int) (layoutLeftDp * density), 0, (int) (layoutRightDp * density), 0);
    textView.setPadding((int) (textLeftDp * density), 0, (int) (textRightDp * density), 0);
    return linearLayout;
}

From source file:com.mishiranu.dashchan.ui.navigator.DrawerForm.java

private View makeHeader(ViewGroup parent, boolean button, float density) {
    if (C.API_LOLLIPOP) {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        View divider = makeSimpleDivider();
        int paddingTop = divider.getPaddingBottom();
        divider.setPadding(divider.getPaddingLeft(), divider.getPaddingTop(), divider.getPaddingRight(), 0);
        linearLayout.addView(divider, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout linearLayout2 = new LinearLayout(context);
        linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(linearLayout2, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        TextView textView = makeCommonTextView(true);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, (int) (32f * density), 1);
        layoutParams.setMargins((int) (16f * density), paddingTop, (int) (16f * density), (int) (8f * density));
        linearLayout2.addView(textView, layoutParams);
        ViewHolder holder = new ViewHolder();
        holder.text = textView;//from w ww  .j av  a 2  s  . co m
        if (button) {
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setBackgroundResource(ResourceUtils.getResourceId(context,
                    android.R.attr.borderlessButtonStyle, android.R.attr.background, 0));
            imageView.setOnClickListener(headerButtonListener);
            imageView.setImageAlpha(0x5e);
            int size = (int) (48f * density);
            layoutParams = new LinearLayout.LayoutParams(size, size);
            layoutParams.rightMargin = (int) (4f * density);
            linearLayout2.addView(imageView, layoutParams);
            holder.extra = imageView;
            holder.icon = imageView;
        }
        linearLayout.setTag(holder);
        return linearLayout;
    } else {
        View view = LayoutInflater.from(context)
                .inflate(ResourceUtils.getResourceId(context, android.R.attr.preferenceCategoryStyle,
                        android.R.attr.layout, android.R.layout.preference_category), parent, false);
        ViewHolder holder = new ViewHolder();
        holder.text = (TextView) view.findViewById(android.R.id.title);
        if (button) {
            int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(measureSpec, measureSpec);
            int size = view.getMeasuredHeight();
            if (size == 0) {
                size = (int) (32f * density);
            }
            FrameLayout frameLayout = new FrameLayout(context);
            frameLayout.addView(view);
            view = frameLayout;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            int padding = (int) (4f * density);
            imageView.setPadding(padding, padding, padding, padding);
            frameLayout.addView(imageView,
                    new FrameLayout.LayoutParams((int) (48f * density), size, Gravity.END));
            View buttonView = new View(context);
            buttonView.setBackgroundResource(
                    ResourceUtils.getResourceId(context, android.R.attr.selectableItemBackground, 0));
            buttonView.setOnClickListener(headerButtonListener);
            frameLayout.addView(buttonView, FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            holder.extra = buttonView;
            holder.icon = imageView;
        }
        view.setTag(holder);
        return view;
    }
}

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int maxGutterSize = measuredWidth / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

    /*//from   w  w w .  j a  va  2 s  . c  om
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {

                final int widthSpec;
                if (this.orientation == LinearLayout.HORIZONTAL) {
                    widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.widthFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, mChildHeightMeasureSpec);

                } else {
                    widthSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.widthFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(mChildWidthMeasureSpec, widthSpec);
                }
            }
        }
    }
}

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    // Make sure scroll position is set correctly.
    if (this.orientation == LinearLayout.HORIZONTAL) {
        if (w != oldw) {
            recomputeScrollPosition(w, oldw, mPageMargin, mPageMargin);
        }/*from  ww w.  ja v a 2 s.c o  m*/
    } else {
        if (w != oldw) {
            recomputeScrollPosition(h, oldh, mPageMargin, mPageMargin);
        }
    }
}

From source file:com.telerik.primitives.ExampleViewPagerBase.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollPosition = getScroll(this);

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }// ww w.  j  ava2s  .  c  o  m
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                if (this.orientation == LinearLayout.HORIZONTAL) {
                    childLeft += scrollPosition;
                } else {
                    childTop += scrollPosition;
                }

                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    final int childLength = this.orientation == LinearLayout.HORIZONTAL ? width - paddingLeft - paddingRight
            : height - paddingTop - paddingBottom;
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (childLength * ii.offset);
                int childLeft = paddingLeft;
                int childTop = paddingTop;

                if (this.orientation == LinearLayout.HORIZONTAL) {
                    childLeft += loff;
                } else {
                    childTop += loff;
                }
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec;
                    final int heightSpec;

                    if (this.orientation == LinearLayout.HORIZONTAL) {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (childLength * lp.widthFactor),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec(height - paddingTop - paddingBottom,
                                MeasureSpec.EXACTLY);
                    } else {
                        widthSpec = MeasureSpec.makeMeasureSpec(width - paddingLeft - paddingRight,
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (childLength * lp.widthFactor),
                                MeasureSpec.EXACTLY);
                    }

                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mTopPageBounds = paddingTop;
    mLeftPageBounds = paddingLeft;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

private void recomputeScrollPosition(int length, int oldLength, int margin, int oldMargin) {
    if (oldLength > 0 && !mItems.isEmpty()) {
        final int lengthWithMargin = length - getPaddingStart(this) - getPaddingEnd(this) + margin;
        final int oldLengthWithMargin = oldLength - getPaddingStart(this) - getPaddingEnd(this) + oldMargin;
        final int scrollPosition = getScroll(this);
        final float pageOffset = (float) scrollPosition / oldLengthWithMargin;
        final int newOffsetPixels = (int) (pageOffset * lengthWithMargin);

        if (this.orientation == LinearLayout.HORIZONTAL) {
            scrollTo(newOffsetPixels, getScrollY());
        } else {//from   w ww  .  j a v  a  2 s.c  o m
            scrollTo(getScrollX(), newOffsetPixels);
        }

        if (!mScroller.isFinished()) {
            // We now return to your regularly scheduled scroll, already in progress.
            final int newDuration = mScroller.getDuration() - mScroller.timePassed();
            ItemInfo targetInfo = infoForPosition(mCurItem);
            if (this.orientation == LinearLayout.HORIZONTAL) {
                mScroller.startScroll(newOffsetPixels, 0, (int) (targetInfo.offset * length), 0, newDuration);
            } else {
                mScroller.startScroll(0, newOffsetPixels, 0, (int) (targetInfo.offset * length), newDuration);
            }
        }
    } else {
        final ItemInfo ii = infoForPosition(mCurItem);
        final float scrollOffset = ii != null ? Math.min(ii.offset, mLastOffset) : 0;
        final int scrollPos = (int) (scrollOffset * (length - getPaddingStart(this) - getPaddingEnd(this)));
        if (scrollPos != getScroll(this)) {
            completeScroll(false);

            if (this.orientation == LinearLayout.HORIZONTAL) {
                scrollTo(scrollPos, getScrollY());
            } else {
                scrollTo(getScrollX(), scrollPos);
            }
        }
    }
}

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollPosition = getScroll(this);

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }//from   w w w.ja v  a  2  s.  c o m
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                if (this.orientation == LinearLayout.HORIZONTAL) {
                    childLeft += scrollPosition;
                } else {
                    childTop += scrollPosition;
                }

                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    final int childLength = this.orientation == LinearLayout.HORIZONTAL ? width - paddingLeft - paddingRight
            : height - paddingTop - paddingBottom;
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (childLength * ii.offset);
                int childLeft = paddingLeft;
                int childTop = paddingTop;

                if (this.orientation == LinearLayout.HORIZONTAL) {
                    childLeft += loff;
                } else {
                    childTop += loff;
                }
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec;
                    final int heightSpec;

                    if (this.orientation == LinearLayout.HORIZONTAL) {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (childLength * lp.widthFactor),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (height - paddingTop - paddingBottom),
                                MeasureSpec.EXACTLY);
                    } else {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (width - paddingLeft - paddingRight),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (childLength * lp.widthFactor),
                                MeasureSpec.EXACTLY);
                    }

                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mTopPageBounds = paddingTop;
    mLeftPageBounds = paddingLeft;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns.//  ww  w  . ja v  a  2  s  .  c om
 *
 * @param position     Position index of the first page currently being displayed.
 *                     Page position+1 will be visible if positionOffset is nonzero.
 * @param offset       Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        final int scroll = getScroll(this);
        int paddingStart = getPaddingStart(this);
        int paddingEnd = getPaddingEnd(this);
        final int length = getViewLength(this);
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (!lp.isDecor)
                continue;

            final int gravity = this.orientation == LinearLayout.HORIZONTAL
                    ? lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK
                    : lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
            int childStart = 0;
            switch (gravity) {
            default:
                childStart = paddingStart;
                break;
            case Gravity.LEFT:
            case Gravity.TOP:
                childStart = paddingStart;
                paddingStart += this.getViewLength(child);
                break;
            case Gravity.CENTER_HORIZONTAL:
            case Gravity.CENTER_VERTICAL:
                childStart = Math.max((length - getViewMeasuredLength(child)) / 2, paddingStart);
                break;
            case Gravity.RIGHT:
            case Gravity.BOTTOM:
                childStart = length - paddingEnd - getViewMeasuredLength(child);
                paddingEnd += getViewMeasuredLength(child);
                break;
            }
            childStart += scroll;

            final int childOffset = childStart - getStart(child);
            if (childOffset != 0) {
                offsetChildStartEnd(child, childOffset);
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final int scroll = getScroll(this);
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (lp.isDecor)
                continue;

            final float transformPos = (float) (getStart(child) - scroll) / getClientLength();
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:com.skytree.epubtest.BookViewActivity.java

public void makeFontBox() {
    int boxColor = Color.rgb(241, 238, 229);
    int innerBoxColor = Color.rgb(246, 244, 239);
    int inlineColor = Color.rgb(133, 105, 75);

    int width = 450;
    int height = 500;
    fontBox = new SkyBox(this);
    fontBox.setBoxColor(boxColor);/*from  w  w  w  .  j  a  v  a 2s  .  c  o m*/
    fontBox.setArrowHeight(ps(25));
    fontBox.setArrowDirection(false);
    setFrame(fontBox, ps(50), ps(200), ps(width), ps(height));

    ScrollView fontBoxScrollView = new ScrollView(this);
    this.setFrame(fontBoxScrollView, ps(5), ps(10), ps(440), ps(height - 50));
    fontBox.contentView.addView(fontBoxScrollView); // NEW

    SkyLayout contentLayout = new SkyLayout(this);
    fontBoxScrollView.addView(contentLayout,
            new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    // #1 first make brightness controller
    // brView is the box containing the bright slider. 
    int FY = 10;
    View brView = new View(this);
    RoundRectShape rrs = new RoundRectShape(
            new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null, null);
    SkyDrawable srd = new SkyDrawable(rrs, innerBoxColor, inlineColor, 1);
    brView.setBackgroundDrawable(srd);
    setFrame(brView, ps(20), ps(FY), ps(width - 40), ps(53));
    contentLayout.addView(brView);
    // darker and brighter icons
    int SBS = 60;
    ImageButton sbb = this.makeImageButton(9005, R.drawable.brightness2x, ps(SBS), ps(SBS));
    setFrame(sbb, ps(50), ps(FY), ps(SBS), ps(SBS));
    sbb.setAlpha(200);
    int BBS = 70;
    ImageButton bbb = this.makeImageButton(9006, R.drawable.brightness2x, ps(BBS), ps(BBS));
    setFrame(bbb, ps(width - 110), ps(FY - 5), ps(BBS), ps(BBS));
    bbb.setAlpha(200);
    contentLayout.addView(sbb);
    contentLayout.addView(bbb);
    // making bright slider      
    brightBar = new SeekBar(this);
    brightBar.setMax(999);
    brightBar.setId(997);
    brightBar.setBackgroundColor(Color.TRANSPARENT);
    brightBar.setOnSeekBarChangeListener(new SeekBarDelegate());
    brightBar.setProgressDrawable(new LineDrawable(Color.rgb(160, 160, 160), ps(10)));
    brightBar.setThumbOffset(-1);
    setFrame(brightBar, ps(100), ps(FY + 4), ps(width - 210), ps(50));
    contentLayout.addView(brightBar);

    // #2 second make decrese/increse font size buttons
    // decrease font size Button
    int FBY = 80;
    decreaseButton = new Button(this);
    setFrame(decreaseButton, ps(20), ps(FBY), ps(width - 40 - 20) / 2, ps(60));
    decreaseButton.setText(getString(R.string.chara));
    decreaseButton.setGravity(Gravity.CENTER);
    decreaseButton.setTextSize(14);
    decreaseButton.setId(5000);
    RoundRectShape drs = new RoundRectShape(
            new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null, null);
    SkyDrawable drd = new SkyDrawable(drs, innerBoxColor, inlineColor, 1);
    decreaseButton.setBackgroundDrawable(drd);
    decreaseButton.setOnClickListener(listener);
    decreaseButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(decreaseButton));
    contentLayout.addView(decreaseButton);
    // inccrease font size Button
    increaseButton = new Button(this);
    setFrame(increaseButton, ps(10 + width / 2), ps(FBY), ps(width - 40 - 20) / 2, ps(60));
    increaseButton.setText(getString(R.string.chara));
    increaseButton.setTextSize(18);
    increaseButton.setGravity(Gravity.CENTER);
    increaseButton.setId(5001);
    RoundRectShape irs = new RoundRectShape(
            new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null, null);
    SkyDrawable ird = new SkyDrawable(irs, innerBoxColor, inlineColor, 1);
    increaseButton.setBackgroundDrawable(ird);
    increaseButton.setOnClickListener(listener);
    increaseButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(increaseButton));
    contentLayout.addView(increaseButton);

    // # 3 make the button to increase/decrese line spacing. 
    int LBY = 145;
    // deccrease line space Button
    decreaseLineSpaceButton = this.makeImageButton(9005, R.drawable.decline2x, ps(30), ps(30));
    setFrame(decreaseLineSpaceButton, ps(20), ps(LBY), ps(width - 40 - 20) / 2, ps(60));
    decreaseLineSpaceButton.setId(4000);
    drs = new RoundRectShape(new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null,
            null);
    drd = new SkyDrawable(drs, innerBoxColor, inlineColor, 1);
    decreaseLineSpaceButton.setBackgroundDrawable(drd);
    decreaseLineSpaceButton.setOnClickListener(listener);
    decreaseLineSpaceButton
            .setOnTouchListener(new ImageButtonHighlighterOnTouchListener(decreaseLineSpaceButton));
    contentLayout.addView(decreaseLineSpaceButton);
    // inccrease line space Button        
    increaseLineSpaceButton = this.makeImageButton(9005, R.drawable.incline2x, ps(30), ps(30));
    setFrame(increaseLineSpaceButton, ps(10 + width / 2), ps(LBY), ps(width - 40 - 20) / 2, ps(60));
    increaseLineSpaceButton.setId(4001);
    irs = new RoundRectShape(new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null,
            null);
    ird = new SkyDrawable(irs, innerBoxColor, inlineColor, 1);
    increaseLineSpaceButton.setBackgroundDrawable(ird);
    increaseLineSpaceButton.setOnClickListener(listener);
    increaseLineSpaceButton
            .setOnTouchListener(new ImageButtonHighlighterOnTouchListener(increaseLineSpaceButton));
    contentLayout.addView(increaseLineSpaceButton);

    // #4 make themes selector.  
    int TY = 220;
    int TH = 70;
    int TW = (width - 40 - 20) / 3;

    HorizontalScrollView themeScrollView = new HorizontalScrollView(this);
    themesView = new LinearLayout(this);
    themesView.setOrientation(LinearLayout.HORIZONTAL);

    themeScrollView.addView(themesView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    for (int i = 0; i < this.themes.size(); i++) {
        Theme theme = themes.get(i);
        Button themeButton = new Button(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ps(TW), ps(TH));
        layoutParams.setMargins(0, 0, 24, 0);
        themesView.addView(themeButton, layoutParams);
        RoundRectShape rs = new RoundRectShape(
                new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null, null);
        SkyDrawable brd = new SkyDrawable(rs, theme.backgroundColor, Color.BLACK, 1);
        themeButton.setBackgroundDrawable(brd);
        themeButton.setText(theme.name);
        themeButton.setTextColor(theme.foregroundColor);
        themeButton.setId(7000 + i);
        themeButton.setOnClickListener(listener);
    }

    this.setFrame(themeScrollView, ps(20), ps(TY), ps(width - 40), ps(90));
    contentLayout.addView(themeScrollView);

    // #5 font list box
    int SY = 310;
    int fontButtonHeight = 80;
    int fontListHeight;
    fontListHeight = fontButtonHeight * fonts.size();
    fontListView = new LinearLayout(this);
    fontListView.setOrientation(LinearLayout.VERTICAL);
    contentLayout.addView(fontListView);
    this.setFrame(fontListView, ps(20), ps(SY), ps(width - 40), ps(fontListHeight));
    int inlineColor2 = Color.argb(140, 133, 105, 75);

    for (int i = 0; i < fonts.size(); i++) {
        CustomFont customFont = fonts.get(i);
        Button fontButton = new Button(this);
        fontButton.setText(customFont.fontFaceName);
        fontButton.setTextSize(20);
        Typeface tf = null;
        if (customFont.fontFileName == null || customFont.fontFileName.isEmpty()) {
            tf = this.getTypeface(customFont.fontFaceName, Typeface.BOLD);
        } else {
            tf = Typeface.createFromAsset(getAssets(), "fonts/" + customFont.fontFileName);
        }
        if (tf != null)
            fontButton.setTypeface(tf);
        fontButton.setId(5100 + i);
        RoundRectShape rs = new RoundRectShape(
                new float[] { ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5), ps(5) }, null, null);
        SkyDrawable brd = new SkyDrawable(rs, innerBoxColor, inlineColor2, 1);
        fontButton.setBackgroundDrawable(brd);
        this.setFrame(fontButton, ps(0), ps(0), ps(width - 40), ps(fontButtonHeight));
        fontListView.addView(fontButton);
        fontButton.setOnClickListener(listener);
        fontButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(fontButton));
    }

    this.ePubView.addView(fontBox);
    this.hideFontBox();
}