Example usage for android.view View setLayoutParams

List of usage examples for android.view View setLayoutParams

Introduction

In this page you can find the example usage for android.view View setLayoutParams.

Prototype

public void setLayoutParams(ViewGroup.LayoutParams params) 

Source Link

Document

Set the layout parameters associated with this view.

Usage

From source file:com.shoshin.paidpay.ExpandingListViewPayVia.java

/**
 * This method collapses the view that was clicked and animates all the views
 * around it to close around the collapsing view. There are several steps required
 * to do this which are outlined below./*from w  w w.j  a v  a  2  s  .c  o m*/
 *
 * 1. Update the layout parameters of the view clicked so as to minimize its height
 *    to the original collapsed (default) state.
 * 2. After invoking a layout, the listview will shift all the cells so as to display
 *    them most efficiently. Therefore, during the first predraw pass, the listview
 *    must be offset by some amount such that given the custom bound change upon
 *    collapse, all the cells that need to be on the screen after the layout
 *    are rendered by the listview.
 * 3. On the second predraw pass, all the items are first returned to their original
 *    location (before the first layout).
 * 4. The collapsing view's bounds are animated to what the final values should be.
 * 5. The bounds above the collapsing view are animated downwards while the bounds
 *    below the collapsing view are animated upwards.
 * 6. The extra text is faded out as its contents become visible throughout the
 *    animation process.
 */

private void collapseView(final View view) {
    final ExpandableCardsWithOffers viewObject = (ExpandableCardsWithOffers) getItemAtPosition(
            getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible.*/
    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                * be determined such that the necessary cells of the ListView are rendered
                * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                    * after the collapse then the bounds are reset to their old values.*/
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            //TODO
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                    * by a cell that was expanded, but not yet collapsed, so its state
                    * should persist in an expanded state with the extra content visible.*/
                    expandingLayout.setAlpha(1);
                    invalidateViews();
                }
            });
            s.start();

            return true;
        }
    });
}

From source file:com.fada21.android.hydralist.expandable.ExpandingListViewDelegate.java

/**
 * This method collapses the view that was clicked and animates all the views around it to close around the collapsing view. There are several steps
 * required to do this which are outlined below.
 * /* w w  w . j a  v a2  s . c  om*/
 * <li>Update the layout parameters of the view clicked so as to minimize its height to the original collapsed (default) state.</li> <li>After invoking a
 * layout, the listview will shift all the cells so as to display them most efficiently. Therefore, during the first predraw pass, the listview must be
 * offset by some amount such that given the custom bound change upon collapse, all the cells that need to be on the screen after the layout are rendered by
 * the listview.</li> <li>On the second predraw pass, all the items are first returned to their original location (before the first layout).</li> <li>The
 * collapsing view's bounds are animated to what the final values should be.</li> <li>The bounds above the collapsing view are animated downwards while the
 * bounds below the collapsing view are animated upwards.</li> <li>The extra text is faded out as its contents become visible throughout the animation
 * process.</li>
 * 
 * @param view
 *            collapsing view
 * @param position
 *            item position in list
 * @param id
 *            item id
 */
private void collapseView(final View view, int position, long id) {
    final ExpandableListItem item = (ExpandableListItem) nlv.getItemAtPosition(nlv.getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells. */
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = nlv.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = nlv.getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible. */
    view.setLayoutParams(
            new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, item.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = nlv.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*
                 * Same as for expandingView, the parameters for setSelectionFromTop must
                 * be determined such that the necessary cells of the ListView are rendered
                 * and added to it.
                 */
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = nlv.getChildAt(0).getTop();
                int firstVisiblePosition = nlv.getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = nlv.getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = nlv.getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                nlv.setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                nlv.requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = nlv.indexOfChild(view);
            int childCount = nlv.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = nlv.getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /*
                     * If the cell was present in the ListView before the collapse and
                     * after the collapse then the bounds are reset to their old values.
                     */
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /*
                     * If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.
                     */
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view
                    .findViewById(getExpandingAdapter().getExpandingHelper().getExpandingLayout());

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = nlv.getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    if (diff != 0f)
                        animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation. */
            nlv.setEnabled(false);
            nlv.setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    item.setExpanded(false);
                    nlv.setEnabled(true);
                    nlv.setClickable(true);
                    /*
                     * Note that alpha must be set back to 1 in case this view is reused
                     * by a cell that was expanded, but not yet collapsed, so its state
                     * should persist in an expanded state with the extra content visible.
                     */
                    expandingLayout.setAlpha(1);
                }
            });
            s.start();

            return true;
        }
    });
}

From source file:com.nbplus.vbroadlauncher.fragment.LauncherFragment.java

private void setContentViewByOrientation() {
    int orientation = DisplayUtils.getScreenOrientation(getActivity());
    LinearLayout.LayoutParams lp;//from  w w w.j  a v a  2s  . c  o  m
    float marginDp = 0f;
    float marginPx = 0f;

    if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            || orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
        mMainViewLayout.setOrientation(LinearLayout.HORIZONTAL);

        lp = (LinearLayout.LayoutParams) mMainViewLeftPanel.getLayoutParams();
        lp.weight = 0.6f;
        marginDp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_panel_margin);
        marginPx = DisplayUtils.pxFromDp(getActivity(), marginDp);
        lp.setMargins(0, 0, (int) marginPx, 0);
        mMainViewLeftPanel.setLayoutParams(lp);
        lp = (LinearLayout.LayoutParams) mMainViewRightPanel.getLayoutParams();
        lp.weight = 0.4f;
        lp.setMargins(0, 0, 0, 0);
        mMainViewRightPanel.setLayoutParams(lp);
    } else {
        mMainViewLayout.setOrientation(LinearLayout.VERTICAL);

        lp = (LinearLayout.LayoutParams) mMainViewLeftPanel.getLayoutParams();
        lp.weight = 1.0f;
        marginDp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_panel_margin);
        marginPx = DisplayUtils.pxFromDp(getActivity(), marginDp);
        lp.setMargins(0, 0, 0, (int) marginPx);
        mMainViewLeftPanel.setLayoutParams(lp);
        lp = (LinearLayout.LayoutParams) mMainViewRightPanel.getLayoutParams();
        lp.weight = 1.0f;
        lp.setMargins(0, 0, 0, 0);
        mMainViewRightPanel.setLayoutParams(lp);
    }
    lp = (LinearLayout.LayoutParams) mMainViewLayout.getLayoutParams();

    float heightDp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_main_view_margin_top);
    float px = DisplayUtils.pxFromDp(getActivity(), heightDp);
    float horizontalMarginDp = DisplayUtils.getDimension(getActivity(),
            R.dimen.launcher_main_view_margin_horizontal);
    float horizontalMarginPx = DisplayUtils.pxFromDp(getActivity(), horizontalMarginDp);

    lp.setMargins((int) horizontalMarginPx, (int) px, (int) horizontalMarginPx, lp.bottomMargin);
    mMainViewLayout.setLayoutParams(lp);

    float dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_clock_height);
    px = DisplayUtils.pxFromDp(getActivity(), dp);
    mTextClock.setTextSize(px);
    mWeatherView.onConfigurationChanged(orientation);

    // right shortcut menu
    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_main_shortcut_width);
    float widthPx = DisplayUtils.pxFromDp(getActivity(), dp);

    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_main_shortcut_height);
    float heightPx = DisplayUtils.pxFromDp(getActivity(), dp);

    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_main_shortcut_font_size);
    float mainShortcutFontPx = DisplayUtils.pxFromDp(getActivity(), dp);

    GridLayout.LayoutParams childlp;
    View child;
    for (int i = 0; i < mMainShortcutGridLayout.getChildCount(); i++) {
        /**
         * right shortcut panel
         */
        child = mMainShortcutGridLayout.getChildAt(i);

        childlp = (GridLayout.LayoutParams) child.getLayoutParams();
        childlp.width = (int) widthPx;
        childlp.height = (int) heightPx;
        child.setLayoutParams(childlp);
    }

    // add other shortcuts.
    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_shortcut_width);
    float btnWidthPx = DisplayUtils.pxFromDp(getActivity(), dp);
    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_shortcut_height);
    float btnHeightPx = DisplayUtils.pxFromDp(getActivity(), dp);

    dp = DisplayUtils.getDimension(getActivity(), R.dimen.ic_nav_btn_drawable_padding);
    float drawablePadding = DisplayUtils.pxFromDp(getActivity(), dp);

    dp = DisplayUtils.getDimension(getActivity(), R.dimen.launcher_ic_menu_shortcut_font_size);
    float btnFontPx = DisplayUtils.pxFromDp(getActivity(), dp);

    for (int i = 0; i < mShorcutGridLayout.getChildCount(); i++) {
        /**
         * right shortcut panel
         */
        child = mShorcutGridLayout.getChildAt(i);

        childlp = (GridLayout.LayoutParams) child.getLayoutParams();
        childlp.width = (int) btnWidthPx;
        childlp.height = (int) btnHeightPx;
        child.setLayoutParams(childlp);
    }
}

From source file:com.jforce.chapelhillnextbus.ExpandingListView.java

/**
 * This method collapses the view that was clicked and animates all the
 * views around it to close around the collapsing view. There are several
 * steps required to do this which are outlined below.
 * <p/>//from   www  . j  a  v a2s  .c om
 * 1. Update the layout parameters of the view clicked so as to minimize its
 * height to the original collapsed (default) state. 2. After invoking a
 * layout, the listview will shift all the cells so as to display them most
 * efficiently. Therefore, during the first predraw pass, the listview must
 * be offset by some amount such that given the custom bound change upon
 * collapse, all the cells that need to be on the screen after the layout
 * are rendered by the listview. 3. On the second predraw pass, all the
 * items are first returned to their original location (before the first
 * layout). 4. The collapsing view's bounds are animated to what the final
 * values should be. 5. The bounds above the collapsing view are animated
 * downwards while the bounds below the collapsing view are animated
 * upwards. 6. The extra text is faded out as its contents become visible
 * throughout the animation process.
 */

private void collapseView(final View view) {
    final ExpandableListItem viewObject = (ExpandableListItem) getItemAtPosition(getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells. */
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible. */
    view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*
                 * Same as for expandingView, the parameters for
                 * setSelectionFromTop must be determined such that the
                 * necessary cells of the ListView are rendered and added to
                 * it.
                 */
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getTopAndBottomTranslations(oldTop, oldBottom, deltaHeight, false);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /*
                     * If the cell was present in the ListView before the
                     * collapse and after the collapse then the bounds are
                     * reset to their old values.
                     */
                    v.setTop(old[0]);
                    v.setBottom(old[1]);
                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /*
                     * If the cell is present in the ListView after the
                     * collapse but not before the collapse then the bounds
                     * are calculated using the bottom and top translation
                     * of the collapsing cell.
                     */
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    v.setTop(v.getTop() + delta);
                    v.setBottom(v.getBottom() + delta);
                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /*
             * Animates all the cells present on the screen after the
             * collapse.
             */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    float diff = i > index ? -yTranslateBottom : yTranslateTop;
                    animations.add(getAnimation(v, diff, diff));
                }
            }

            /* Adds animation for collapsing the cell that was clicked. */
            animations.add(getAnimation(view, yTranslateTop, -yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            animations.add(ObjectAnimator.ofFloat(expandingLayout, View.ALPHA, 1, 0));

            /* Disabled the ListView for the duration of the animation. */
            setEnabled(false);
            setClickable(false);

            /*
             * Play all the animations created above together at the same
             * time.
             */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(
                            new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /*
                     * Note that alpha must be set back to 1 in case this
                     * view is reused by a cell that was expanded, but not
                     * yet collapsed, so its state should persist in an
                     * expanded state with the extra content visible.
                     */
                    expandingLayout.setAlpha(1);
                }
            });
            s.start();

            return true;
        }
    });
}

From source file:com.crearo.gpslogger.GpsMainActivity.java

public void setupEvenlyDistributedToolbar() {
    //http://stackoverflow.com/questions/26489079/evenly-spaced-menu-items-on-toolbar

    // Use Display metrics to get Screen Dimensions
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);/*ww  w . ja v a  2 s.c om*/

    // Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarBottom);

    // Add 10 spacing on either side of the toolbar
    toolbar.setContentInsetsAbsolute(10, 10);

    // Get the ChildCount of your Toolbar, this should only be 1
    int childCount = toolbar.getChildCount();
    // Get the Screen Width in pixels
    int screenWidth = metrics.widthPixels;

    // Create the Toolbar Params based on the screenWidth
    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth,
            Toolbar.LayoutParams.WRAP_CONTENT);

    // Loop through the child Items
    for (int i = 0; i < childCount; i++) {
        // Get the item at the current index
        View childView = toolbar.getChildAt(i);
        // If its a ViewGroup
        if (childView instanceof ViewGroup) {
            // Set its layout params
            childView.setLayoutParams(toolbarParams);
            // Get the child count of this view group, and compute the item widths based on this count & screen size
            int innerChildCount = ((ViewGroup) childView).getChildCount();
            int itemWidth = (screenWidth / innerChildCount);
            // Create layout params for the ActionMenuView
            ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth,
                    Toolbar.LayoutParams.WRAP_CONTENT);
            // Loop through the children
            for (int j = 0; j < innerChildCount; j++) {
                View grandChild = ((ViewGroup) childView).getChildAt(j);
                if (grandChild instanceof ActionMenuItemView) {
                    // set the layout parameters on each View
                    grandChild.setLayoutParams(params);
                }
            }
        }
    }
}

From source file:com.byds.kd.views.SlidingTabLayout.java

private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;
        TextView tabTitleView = null;//from  ww  w . j  a v  a  2  s  . co  m

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        if (mDistributeEvenly) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
            lp.width = 0;
            lp.weight = 1;
        }

        tabTitleView.setText(adapter.getPageTitle(i));
        tabView.setOnClickListener(tabClickListener);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,
                1.0f);
        tabView.setLayoutParams(layoutParams);
        String desc = mContentDescriptions.get(i, null);
        if (desc != null) {
            tabView.setContentDescription(desc);
        }

        mTabStrip.addView(tabView);
        if (i == mViewPager.getCurrentItem()) {
            tabView.setSelected(true);
        }
    }
}

From source file:com.qs.qswlw.view.Mypager.UltraViewPagerAdapter.java

@Override
public Object instantiateItem(ViewGroup container, int position) {
    int realPosition = position;
    //TODO/*from  w  w w.j  ava 2 s  . c o  m*/
    if (enableLoop && adapter.getCount() != 0) {
        realPosition = position % adapter.getCount();
    }

    Object item = adapter.instantiateItem(container, realPosition);
    //TODO
    View childView = null;
    if (item instanceof View)
        childView = (View) item;
    //        if (item instanceof RecyclerView.ViewHolder)
    //            childView = ((RecyclerView.ViewHolder) item).itemView;

    ViewPager viewPager = (ViewPager) container;
    int childCount = viewPager.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = viewPager.getChildAt(i);
        if (isViewFromObject(child, item)) {
            viewArray.put(realPosition, child);
            break;
        }
    }

    if (isEnableMultiScr()) {
        if (scrWidth == 0) {
            scrWidth = container.getResources().getDisplayMetrics().widthPixels;
        }
        RelativeLayout relativeLayout = new RelativeLayout(container.getContext());

        if (childView.getLayoutParams() != null) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    (int) (scrWidth * multiScrRatio), ViewGroup.LayoutParams.MATCH_PARENT);

            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            childView.setLayoutParams(layoutParams);
        }

        container.removeView(childView);
        relativeLayout.addView(childView);

        container.addView(relativeLayout);
        return relativeLayout;
    }

    return item;
}

From source file:ac.robinson.mediaphone.activity.NarrativeBrowserActivity.java

private void initialiseNarrativesView() {
    mScanningForNarratives = false;// w ww .  j a  va 2  s . c  om
    mNarratives = (NarrativesListView) findViewById(R.id.list_narratives);

    // for API 11 and above, buttons are in the action bar
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View headerRow = layoutInflater.inflate(R.layout.narratives_header, null, false);
        mNarratives.addHeaderView(headerRow, null, false); // false = not selectable
        View emptyView = layoutInflater.inflate(R.layout.narratives_empty, null, false);
        ((ViewGroup) mNarratives.getParent()).addView(emptyView);
        mNarratives.setEmptyView(emptyView); // must add separately as the header isn't shown when empty

    } else {
        // initial empty list placeholder - add manually as the < v11 version includes the header row
        TextView emptyView = new TextView(NarrativeBrowserActivity.this);
        emptyView.setGravity(Gravity.CENTER | Gravity.TOP);
        emptyView.setPadding(10,
                getResources().getDimensionPixelSize(R.dimen.narrative_list_empty_hint_top_padding), 10, 10); // temporary
        emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        emptyView.setText(getString(R.string.narrative_list_empty));
        ((ViewGroup) mNarratives.getParent()).addView(emptyView);
        mNarratives.setEmptyView(emptyView);
    }

    // originally used to fix selection highlights when using hardware button to select
    // now done by overriding isEnabled in NarrativeAdapter
    // mNarratives.setFocusable(false);
    // mNarratives.setFocusableInTouchMode(false);

    mNarrativeAdapter = new NarrativeAdapter(this, true, false);
    mNarratives.setAdapter(mNarrativeAdapter);
    getSupportLoaderManager().initLoader(R.id.loader_narratives_completed, null, this);
    mNarratives.setOnScrollListener(new ScrollManager());
    mNarratives.setOnTouchListener(new FingerTracker());
    mNarratives.setOnItemSelectedListener(new SelectionTracker());
    mNarratives.setOnItemClickListener(new NarrativeViewer());

    mPopupPosition = getLayoutInflater().inflate(R.layout.popup_position, null);
    mPopupText = (TextView) mPopupPosition.findViewById(R.id.popup_text);
}

From source file:com.example.android.expandingcells.ExpandingListView.java

/**
 * This method collapses the view that was clicked and animates all the views
 * around it to close around the collapsing view. There are several steps required
 * to do this which are outlined below./*from   w  w  w .  j ava 2 s  .  c om*/
 *
 * 1. Update the layout parameters of the view clicked so as to minimize its height
 *    to the original collapsed (default) state.
 * 2. After invoking a layout, the listview will shift all the cells so as to display
 *    them most efficiently. Therefore, during the first predraw pass, the listview
 *    must be offset by some amount such that given the custom bound change upon
 *    collapse, all the cells that need to be on the screen after the layout
 *    are rendered by the listview.
 * 3. On the second predraw pass, all the items are first returned to their original
 *    location (before the first layout).
 * 4. The collapsing view's bounds are animated to what the final values should be.
 * 5. The bounds above the collapsing view are animated downwards while the bounds
 *    below the collapsing view are animated upwards.
 * 6. The extra text is faded out as its contents become visible throughout the
 *    animation process.
 */
private void collapseView(final View view) {
    final ExpandableListItem viewObject = (ExpandableListItem) getItemAtPosition(getPositionForView(view));

    /* Store the original top and bottom bounds of all the cells.*/
    final int oldTop = view.getTop();
    final int oldBottom = view.getBottom();

    int childCount = getChildCount();
    final HashMap<View, int[]> oldCoordinates = new HashMap<View, int[]>();

    for (int i = 0; i < childCount; i++) {
        View v = getChildAt(i);
        ViewCompat.setHasTransientState(v, true);
        oldCoordinates.put(v, new int[] { v.getTop(), v.getBottom() });
    }

    /* Update the layout so the extra content becomes invisible.*/
    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            viewObject.getCollapsedHeight()));

    /* Add an onPreDraw listener. */
    final ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {

            if (!mShouldRemoveObserver) {
                /*Same as for expandingView, the parameters for setSelectionFromTop must
                 * be determined such that the necessary cells of the ListView are rendered
                 * and added to it.*/
                mShouldRemoveObserver = true;

                int newTop = view.getTop();
                int newBottom = view.getBottom();

                int newHeight = newBottom - newTop;
                int oldHeight = oldBottom - oldTop;
                int deltaHeight = oldHeight - newHeight;

                mTranslate = getCollapsingTopAndBottomTranslation(getPositionForView(view), oldTop, oldBottom,
                        deltaHeight);

                int currentTop = view.getTop();
                int futureTop = oldTop + mTranslate[0];

                int firstChildStartTop = getChildAt(0).getTop();
                int firstVisiblePosition = getFirstVisiblePosition();
                int deltaTop = currentTop - futureTop;

                int i;
                int childCount = getChildCount();
                for (i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    int height = v.getBottom() - Math.max(0, v.getTop());
                    if (deltaTop - height > 0) {
                        firstVisiblePosition++;
                        deltaTop -= height;
                    } else {
                        break;
                    }
                }

                if (i > 0) {
                    firstChildStartTop = 0;
                }

                setSelectionFromTop(firstVisiblePosition, firstChildStartTop - deltaTop);

                requestLayout();

                return false;
            }

            mShouldRemoveObserver = false;
            observer.removeOnPreDrawListener(this);

            int yTranslateTop = mTranslate[0];
            int yTranslateBottom = mTranslate[1];

            int index = indexOfChild(view);
            int childCount = getChildCount();

            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                int[] old = oldCoordinates.get(v);
                if (old != null) {
                    /* If the cell was present in the ListView before the collapse and
                     * after the collapse then the bounds are reset to their old values.*/
                    handleSetTopAndBottom(v, old[0], old[1]);

                    ViewCompat.setHasTransientState(v, false);
                } else {
                    /* If the cell is present in the ListView after the collapse but
                     * not before the collapse then the bounds are calculated using
                     * the bottom and top translation of the collapsing cell.*/
                    int delta = i > index ? yTranslateBottom : -yTranslateTop;
                    handleSetTopAndBottom(v, v.getTop() + delta, v.getBottom() + delta);
                }
            }

            /* Animates all the cells present on the screen after the collapse. */
            ArrayList<Animator> animations = new ArrayList<Animator>();
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt(i);
                if (v != view) {
                    int old[] = oldCoordinates.get(v);
                    float diff = i > index ? yTranslateBottom : yTranslateTop;
                    if (old != null) {
                        animations.add(getAnimation(v, old[0], old[1], diff, diff));
                    } else {
                        animations.add(getAnimation(v, v.getTop() - (int) diff, v.getBottom() - (int) diff,
                                diff, diff));
                    }

                }
            }

            final View expandingLayout = view.findViewById(R.id.expanding_layout);

            /* Adds animation for collapsing the cell that was clicked. */
            int old[] = oldCoordinates.get(view);
            animations.add(getAnimation(view, old[0], old[1], yTranslateTop, (float) yTranslateBottom));

            /* Adds an animation for fading out the extra content. */
            animations.add(ObjectAnimator.ofFloat(expandingLayout, "alpha", 1, 0));

            /* Disabled the ListView for the duration of the animation.*/
            setEnabled(false);
            setClickable(false);

            /* Play all the animations created above together at the same time. */
            AnimatorSet s = new AnimatorSet();
            s.playTogether(animations);
            s.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    expandingLayout.setVisibility(View.GONE);
                    view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                            AbsListView.LayoutParams.WRAP_CONTENT));
                    viewObject.setExpanded(false);
                    setEnabled(true);
                    setClickable(true);
                    /* Note that alpha must be set back to 1 in case this view is reused
                     * by a cell that was expanded, but not yet collapsed, so its state
                     * should persist in an expanded state with the extra content visible.*/
                    ViewHelper.setAlpha(expandingLayout, 1);

                    if (mOnExpandingListener != null) {
                        mOnExpandingListener.onCollasped(view);
                    }
                }
            });
            s.start();

            return true;
        }
    });
}