com.mikepenz.materialize.util.UIUtils Java Examples

The following examples show how to use com.mikepenz.materialize.util.UIUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BadgeStyle.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
public void style(TextView badgeTextView, ColorStateList colorStateList) {
    Context ctx = badgeTextView.getContext();
    //set background for badge
    if (mBadgeBackground == null) {
        UIUtils.setBackground(badgeTextView, new BadgeDrawableBuilder(this).build(ctx));
    } else {
        UIUtils.setBackground(badgeTextView, mBadgeBackground);
    }

    //set the badge text color
    if (mTextColor != null) {
        ColorHolder.applyToOr(mTextColor, badgeTextView, null);
    } else if (colorStateList != null) {
        badgeTextView.setTextColor(colorStateList);
    }

    //set the padding
    int paddingLeftRight = mPaddingLeftRight.asPixel(ctx);
    int paddingTopBottom = mPaddingTopBottom.asPixel(ctx);
    badgeTextView.setPadding(paddingLeftRight, paddingTopBottom, paddingLeftRight, paddingTopBottom);

    //set the min width
    badgeTextView.setMinWidth(mMinWidth.asPixel(ctx));
}
 
Example #2
Source File: ImageHolder.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
/**
 * this only handles Drawables
 *
 * @param ctx
 * @param iconColor
 * @param tint
 * @return
 */
public Drawable decideIcon(Context ctx, int iconColor, boolean tint, int paddingDp) {
    Drawable icon = getIcon();

    if (mIIcon != null) {
        icon = new IconicsDrawable(ctx, mIIcon).color(iconColor).sizeDp(24).paddingDp(paddingDp);
    } else if (getIconRes() != -1) {
        icon = UIUtils.getCompatDrawable(ctx, getIconRes());
    } else if (getUri() != null) {
        try {
            InputStream inputStream = ctx.getContentResolver().openInputStream(getUri());
            icon = Drawable.createFromStream(inputStream, getUri().toString());
        } catch (FileNotFoundException e) {
            //no need to handle this
        }
    }

    //if we got an icon AND we have auto tinting enabled AND it is no IIcon, tint it ;)
    if (icon != null && tint && mIIcon == null) {
        icon = icon.mutate();
        icon.setColorFilter(iconColor, PorterDuff.Mode.SRC_IN);
    }

    return icon;
}
 
Example #3
Source File: BadgeDrawableBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
public StateListDrawable build(Context ctx) {
    StateListDrawable stateListDrawable = new StateListDrawable();

    GradientDrawable normal = (GradientDrawable) UIUtils.getCompatDrawable(ctx, mStyle.getGradientDrawable());
    GradientDrawable selected = (GradientDrawable) normal.getConstantState().newDrawable().mutate();

    ColorHolder.applyToOrTransparent(mStyle.getColor(), ctx, normal);
    if (mStyle.getColorPressed() == null) {
        ColorHolder.applyToOrTransparent(mStyle.getColor(), ctx, selected);
    } else {
        ColorHolder.applyToOrTransparent(mStyle.getColorPressed(), ctx, selected);
    }

    if (mStyle.getCorners() != null) {
        normal.setCornerRadius(mStyle.getCorners().asPixel(ctx));
        selected.setCornerRadius(mStyle.getCorners().asPixel(ctx));
    }

    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selected);
    stateListDrawable.addState(StateSet.WILD_CARD, normal);

    return stateListDrawable;
}
 
Example #4
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
/**
 * build the sticky footer item view
 *
 * @return
 */
public static ViewGroup buildStickyDrawerItemFooter(Context ctx, DrawerBuilder drawer, View.OnClickListener onClickListener) {
    //create the container view
    final LinearLayout linearLayout = new LinearLayout(ctx);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    //set the background color to the drawer background color (if it has alpha the shadow won't be visible)
    linearLayout.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_background, R.color.material_drawer_background));

    //create the divider
    if (drawer.mStickyFooterDivider) {
        addStickyFooterDivider(ctx, linearLayout);
    }

    fillStickyDrawerItemFooter(drawer, linearLayout, onClickListener);

    return linearLayout;
}
 
Example #5
Source File: DividerDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
@Override
public void bindView(RecyclerView.ViewHolder holder) {
    Context ctx = holder.itemView.getContext();

    //get our viewHolder
    ViewHolder viewHolder = (ViewHolder) holder;

    //set the identifier from the drawerItem here. It can be used to run tests
    holder.itemView.setId(getIdentifier());

    //define how the divider should look like
    viewHolder.view.setClickable(false);
    viewHolder.view.setEnabled(false);
    viewHolder.view.setMinimumHeight(1);
    ViewCompat.setImportantForAccessibility(viewHolder.view,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);

    //set the color for the divider
    viewHolder.divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_divider, R.color.material_drawer_divider));

    //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
    onPostBindView(this, holder.itemView);
}
 
Example #6
Source File: AccountHeaderBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 6 votes vote down vote up
/**
 * a small helper to handle the selectionView
 *
 * @param on
 */
private void handleSelectionView(IProfile profile, boolean on) {
    if (on) {
        if (Build.VERSION.SDK_INT >= 21) {
            ((FrameLayout) mAccountHeaderContainer).setForeground(UIUtils.getCompatDrawable(mAccountHeaderContainer.getContext(), mAccountHeaderTextSectionBackgroundResource));
            mAccountHeaderContainer.setOnClickListener(onSelectionClickListener);
            mAccountHeaderContainer.setTag(R.id.material_drawer_profile_header, profile);
        } else {
            mAccountHeaderTextSection.setBackgroundResource(mAccountHeaderTextSectionBackgroundResource);
            mAccountHeaderTextSection.setOnClickListener(onSelectionClickListener);
            mAccountHeaderTextSection.setTag(R.id.material_drawer_profile_header, profile);
        }
    } else {
        if (Build.VERSION.SDK_INT >= 21) {
            ((FrameLayout) mAccountHeaderContainer).setForeground(null);
            mAccountHeaderContainer.setOnClickListener(null);
        } else {
            UIUtils.setBackground(mAccountHeaderTextSection, null);
            mAccountHeaderTextSection.setOnClickListener(null);
        }
    }
}
 
Example #7
Source File: DrawerBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * build the drawers content only. This will still return a Result object, but only with the content set. No inflating of a DrawerLayout.
 *
 * @return Result object with only the content set
 */
public Drawer buildView() {
    // get the slider view
    mSliderLayout = (RelativeLayout) mActivity.getLayoutInflater().inflate(R.layout.material_drawer_slider, mDrawerLayout, false);
    mSliderLayout.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
    // get the layout params
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mSliderLayout.getLayoutParams();
    if (params != null) {
        // if we've set a custom gravity set it
        params.gravity = mDrawerGravity;
        // if this is a drawer from the right, change the margins :D
        params = DrawerUtils.processDrawerLayoutParams(this, params);
        // set the new layout params
        mSliderLayout.setLayoutParams(params);
    }

    //create the content
    createContent();

    //create the result object
    Drawer result = new Drawer(this);
    //set the drawer for the accountHeader if set
    if (mAccountHeader != null) {
        mAccountHeader.setDrawer(result);
    }

    //toggle selection list if we were previously on the account list
    if (mSavedInstance != null && mSavedInstance.getBoolean(Drawer.BUNDLE_DRAWER_CONTENT_SWITCHED, false)) {
        mAccountHeader.toggleSelectionList(mActivity);
    }

    //handle if the drawer should be shown on first launch
    handleShowOnFirstLaunch();

    //forget the reference to the activity
    mActivity = null;

    return result;
}
 
Example #8
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * helper to extend the layoutParams of the drawer
 *
 * @param params
 * @return
 */
public static DrawerLayout.LayoutParams processDrawerLayoutParams(DrawerBuilder drawer, DrawerLayout.LayoutParams params) {
    if (params != null) {
        if (drawer.mDrawerGravity != null && (drawer.mDrawerGravity == Gravity.RIGHT || drawer.mDrawerGravity == Gravity.END)) {
            params.rightMargin = 0;
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(0);
            }

            params.leftMargin = drawer.mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_margin);
            if (Build.VERSION.SDK_INT >= 17) {
                params.setMarginEnd(drawer.mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_margin));
            }
        }

        if (drawer.mDisplayBelowStatusBar != null && drawer.mDisplayBelowStatusBar && Build.VERSION.SDK_INT >= 19) {
            params.topMargin = UIUtils.getStatusBarHeight(drawer.mActivity, true);
        }

        if (drawer.mDrawerWidth > -1) {
            params.width = drawer.mDrawerWidth;
        } else {
            params.width = DrawerUIUtils.getOptimalDrawerWidth(drawer.mActivity);
        }
    }

    return params;
}
 
Example #9
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * helper method to fill the sticky footer with it's elements
 *
 * @param drawer
 * @param container
 * @param onClickListener
 */
public static void fillStickyDrawerItemFooter(DrawerBuilder drawer, ViewGroup container, View.OnClickListener onClickListener) {
    //add all drawer items
    for (IDrawerItem drawerItem : drawer.mStickyDrawerItems) {
        //get the selected_color
        int selected_color = UIUtils.getThemeColorFromAttrOrRes(container.getContext(), R.attr.material_drawer_selected, R.color.material_drawer_selected);
        if (drawerItem instanceof PrimaryDrawerItem) {
            selected_color = ColorHolder.color(((PrimaryDrawerItem) drawerItem).getSelectedColor(), container.getContext(), R.attr.material_drawer_selected, R.color.material_drawer_selected);
        } else if (drawerItem instanceof SecondaryDrawerItem) {
            selected_color = ColorHolder.color(((SecondaryDrawerItem) drawerItem).getSelectedColor(), container.getContext(), R.attr.material_drawer_selected, R.color.material_drawer_selected);
        }

        View view = drawerItem.generateView(container.getContext(), container);
        view.setTag(drawerItem);

        if (drawerItem.isEnabled()) {
            UIUtils.setBackground(view, DrawerUIUtils.getSelectableBackground(container.getContext(), selected_color));
            view.setOnClickListener(onClickListener);
        }

        container.addView(view);

        //for android API 17 --> Padding not applied via xml
        DrawerUIUtils.setDrawerVerticalPadding(view);
    }
    //and really. don't ask about this. it won't set the padding if i don't set the padding for the container
    container.setPadding(0, 0, 0, 0);
}
 
Example #10
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
/**
 * adds the shadow to the stickyFooter
 *
 * @param ctx
 * @param footerView
 */
private static void addStickyFooterDivider(Context ctx, ViewGroup footerView) {
    LinearLayout divider = new LinearLayout(ctx);
    LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    //remove bottomMargin --> See inbox it also has no margin here
    //dividerParams.bottomMargin = mActivity.getResources().getDimensionPixelSize(R.dimen.material_drawer_padding);
    divider.setMinimumHeight((int) UIUtils.convertDpToPixel(1, ctx));
    divider.setOrientation(LinearLayout.VERTICAL);
    divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_divider, R.color.material_drawer_divider));
    footerView.addView(divider, dividerParams);
}
 
Example #11
Source File: SectionDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
@Override
public void bindView(RecyclerView.ViewHolder holder) {
    Context ctx = holder.itemView.getContext();

    //get our viewHolder
    ViewHolder viewHolder = (ViewHolder) holder;

    //set the identifier from the drawerItem here. It can be used to run tests
    holder.itemView.setId(getIdentifier());

    //define this item to be not clickable nor enabled
    viewHolder.view.setClickable(false);
    viewHolder.view.setEnabled(false);

    //define the text color
    viewHolder.name.setTextColor(ColorHolder.color(getTextColor(), ctx, R.attr.material_drawer_secondary_text, R.color.material_drawer_secondary_text));

    //set the text for the name
    StringHolder.applyTo(this.getName(), viewHolder.name);

    //hide the divider if we do not need one
    if (this.hasDivider()) {
        viewHolder.divider.setVisibility(View.VISIBLE);
    } else {
        viewHolder.divider.setVisibility(View.GONE);
    }

    //set the color for the divider
    viewHolder.divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_divider, R.color.material_drawer_divider));

    //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
    onPostBindView(this, holder.itemView);
}
 
Example #12
Source File: DrawerItemViewHelper.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
public View build() {
    //create the container view
    LinearLayout linearLayout = new LinearLayout(mContext);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    //create the divider
    if (mDivider) {
        LinearLayout divider = new LinearLayout(mContext);
        divider.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        divider.setMinimumHeight((int) UIUtils.convertDpToPixel(1, mContext));
        divider.setOrientation(LinearLayout.VERTICAL);
        divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mContext, R.attr.material_drawer_divider, R.color.material_drawer_divider));
        linearLayout.addView(divider);
    }

    //add all drawer items
    for (IDrawerItem drawerItem : mDrawerItems) {
        View view = drawerItem.generateView(mContext);
        view.setTag(drawerItem);

        if (drawerItem.isEnabled()) {
            view.setBackgroundResource(DrawerUIUtils.getSelectableBackground(mContext));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnDrawerItemClickListener != null) {
                        mOnDrawerItemClickListener.onItemClick(v, (IDrawerItem) v.getTag());
                    }
                }
            });
        }

        linearLayout.addView(view);
    }

    return linearLayout;
}
 
Example #13
Source File: KeyboardUtil.java    From MaterialDrawer-Xamarin with Apache License 2.0 5 votes vote down vote up
@Override
public void onGlobalLayout() {
    Rect r = new Rect();
    //r will be populated with the coordinates of your view that area still visible.
    decorView.getWindowVisibleDisplayFrame(r);

    //get the height diff as dp
    float heightDiffDp = UIUtils.convertPixelsToDp(decorView.getRootView().getHeight() - (r.bottom - r.top), decorView.getContext());

    //set the initialDpDiff at the beginning. (on my phone this was 73dp)
    if (initialDpDiff == -1) {
        initialDpDiff = heightDiffDp;
    }

    //if it could be a keyboard add the padding to the view
    if (heightDiffDp - initialDpDiff > 100) { // if more than 100 pixels, its probably a keyboard...
        //check if the padding is 0 (if yes set the padding for the keyboard)
        if (contentView.getPaddingBottom() == 0) {
            //set the padding of the contentView for the keyboard
            contentView.setPadding(0, 0, 0, (int) UIUtils.convertDpToPixel((heightDiffDp - initialDpDiff), decorView.getContext()));
        }
    } else {
        //check if the padding is != 0 (if yes reset the padding)
        if (contentView.getPaddingBottom() != 0) {
            //reset the padding of the contentView
            contentView.setPadding(0, 0, 0, 0);
        }
    }
}
 
Example #14
Source File: MiniDrawer.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * build the MiniDrawer
 *
 * @param ctx
 * @return
 */
public View build(Context ctx) {
    mContainer = new LinearLayout(ctx);
    if (mInnerShadow) {
        if (!mInRTL) {
            mContainer.setBackgroundResource(R.drawable.material_drawer_shadow_left);
        } else {
            mContainer.setBackgroundResource(R.drawable.material_drawer_shadow_right);
        }
    }

    //create and append recyclerView
    mRecyclerView = new RecyclerView(ctx);
    mContainer.addView(mRecyclerView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    //set the itemAnimator
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    //some style improvements on older devices
    mRecyclerView.setFadingEdgeLength(0);
    //set the drawing cache background to the same color as the slider to improve performance
    //mRecyclerView.setDrawingCacheBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
    mRecyclerView.setClipToPadding(false);
    //additional stuff
    mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
    //adapter
    mDrawerAdapter = new DrawerAdapter();
    mRecyclerView.setAdapter(mDrawerAdapter);

    //if the activity with the drawer should be fullscreen add the padding for the statusbar
    if (mDrawer != null && mDrawer.mDrawerBuilder != null && (mDrawer.mDrawerBuilder.mFullscreen || mDrawer.mDrawerBuilder.mTranslucentStatusBar)) {
        mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), UIUtils.getStatusBarHeight(ctx), mRecyclerView.getPaddingRight(), mRecyclerView.getPaddingBottom());
    }

    //if the activity with the drawer should be fullscreen add the padding for the navigationBar
    if (mDrawer != null && mDrawer.mDrawerBuilder != null && (mDrawer.mDrawerBuilder.mFullscreen || mDrawer.mDrawerBuilder.mTranslucentNavigationBar)) {
        mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(), mRecyclerView.getPaddingRight(), UIUtils.getNavigationBarHeight(ctx));
    }

    //set the adapter with the items
    createItems();

    return mContainer;
}
 
Example #15
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * helper method to handle the footerView
 *
 * @param drawer
 */
public static void handleFooterView(DrawerBuilder drawer, View.OnClickListener onClickListener) {
    Context ctx = drawer.mSliderLayout.getContext();

    //use the StickyDrawerItems if set
    if (drawer.mStickyDrawerItems != null && drawer.mStickyDrawerItems.size() > 0) {
        drawer.mStickyFooterView = DrawerUtils.buildStickyDrawerItemFooter(ctx, drawer, onClickListener);
    }

    //sticky footer view
    if (drawer.mStickyFooterView != null) {
        //add the sticky footer view and align it to the bottom
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
        drawer.mStickyFooterView.setId(R.id.material_drawer_sticky_footer);
        drawer.mSliderLayout.addView(drawer.mStickyFooterView, layoutParams);

        if ((drawer.mTranslucentNavigationBar || drawer.mFullscreen) && Build.VERSION.SDK_INT >= 19) {
            drawer.mStickyFooterView.setPadding(0, 0, 0, UIUtils.getNavigationBarHeight(ctx));
        }

        //now align the recyclerView above the stickyFooterView ;)
        RelativeLayout.LayoutParams layoutParamsListView = (RelativeLayout.LayoutParams) drawer.mRecyclerView.getLayoutParams();
        layoutParamsListView.addRule(RelativeLayout.ABOVE, R.id.material_drawer_sticky_footer);
        drawer.mRecyclerView.setLayoutParams(layoutParamsListView);

        //handle shadow on top of the sticky footer
        if (drawer.mStickyFooterShadow) {
            drawer.mStickyFooterShadowView = new View(ctx);
            drawer.mStickyFooterShadowView.setBackgroundResource(R.drawable.material_drawer_shadow_top);
            drawer.mSliderLayout.addView(drawer.mStickyFooterShadowView, RelativeLayout.LayoutParams.MATCH_PARENT, (int) UIUtils.convertDpToPixel(4, ctx));
            //now align the shadow below the stickyHeader ;)
            RelativeLayout.LayoutParams lps = (RelativeLayout.LayoutParams) drawer.mStickyFooterShadowView.getLayoutParams();
            lps.addRule(RelativeLayout.ABOVE, R.id.material_drawer_sticky_footer);
            drawer.mStickyFooterShadowView.setLayoutParams(lps);
        }

        //remove the padding of the recyclerView again we have the footer below it
        drawer.mRecyclerView.setPadding(drawer.mRecyclerView.getPaddingLeft(), drawer.mRecyclerView.getPaddingTop(), drawer.mRecyclerView.getPaddingRight(), ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_padding));
    }

    // set the footer (do this before the setAdapter because some devices will crash else
    if (drawer.mFooterView != null) {
        if (drawer.mRecyclerView == null) {
            throw new RuntimeException("can't use a footerView without a recyclerView");
        }

        if (drawer.mFooterDivider) {
            drawer.getAdapter().addFooterDrawerItems(new ContainerDrawerItem().withView(drawer.mFooterView).withViewPosition(ContainerDrawerItem.Position.BOTTOM));
        } else {
            drawer.getAdapter().addFooterDrawerItems(new ContainerDrawerItem().withView(drawer.mFooterView).withViewPosition(ContainerDrawerItem.Position.NONE));
        }
    }
}
 
Example #16
Source File: DrawerUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * helper method to handle the headerView
 *
 * @param drawer
 */
public static void handleHeaderView(DrawerBuilder drawer) {
    //use the AccountHeader if set
    if (drawer.mAccountHeader != null) {
        if (drawer.mAccountHeaderSticky) {
            drawer.mStickyHeaderView = drawer.mAccountHeader.getView();
        } else {
            drawer.mHeaderView = drawer.mAccountHeader.getView();
            drawer.mHeaderDivider = drawer.mAccountHeader.mAccountHeaderBuilder.mDividerBelowHeader;
            drawer.mHeaderPadding = drawer.mAccountHeader.mAccountHeaderBuilder.mPaddingBelowHeader;
        }
    }

    //sticky header view
    if (drawer.mStickyHeaderView != null) {
        //add the sticky footer view and align it to the bottom
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
        drawer.mStickyHeaderView.setId(R.id.material_drawer_sticky_header);
        drawer.mSliderLayout.addView(drawer.mStickyHeaderView, 0, layoutParams);

        //now align the recyclerView below the stickyFooterView ;)
        RelativeLayout.LayoutParams layoutParamsListView = (RelativeLayout.LayoutParams) drawer.mRecyclerView.getLayoutParams();
        layoutParamsListView.addRule(RelativeLayout.BELOW, R.id.material_drawer_sticky_header);
        drawer.mRecyclerView.setLayoutParams(layoutParamsListView);

        //set a background color or the elevation will not work
        drawer.mStickyHeaderView.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(drawer.mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));

        //add a shadow
        if (Build.VERSION.SDK_INT >= 21) {
            drawer.mStickyHeaderView.setElevation(UIUtils.convertDpToPixel(4, drawer.mActivity));
        } else {
            View view = new View(drawer.mActivity);
            view.setBackgroundResource(R.drawable.material_drawer_shadow_bottom);
            drawer.mSliderLayout.addView(view, RelativeLayout.LayoutParams.MATCH_PARENT, (int) UIUtils.convertDpToPixel(4, drawer.mActivity));
            //now align the shadow below the stickyHeader ;)
            RelativeLayout.LayoutParams lps = (RelativeLayout.LayoutParams) view.getLayoutParams();
            lps.addRule(RelativeLayout.BELOW, R.id.material_drawer_sticky_header);
            view.setLayoutParams(lps);
        }

        //remove the padding of the recyclerView again we have the header on top of it
        drawer.mRecyclerView.setPadding(0, 0, 0, 0);
    }

    // set the header (do this before the setAdapter because some devices will crash else
    if (drawer.mHeaderView != null) {
        if (drawer.mRecyclerView == null) {
            throw new RuntimeException("can't use a headerView without a recyclerView");
        }

        if (drawer.mHeaderPadding) {
            drawer.getAdapter().addHeaderDrawerItems(new ContainerDrawerItem().withView(drawer.mHeaderView).withDivider(drawer.mHeaderDivider).withViewPosition(ContainerDrawerItem.Position.TOP));
        } else {
            drawer.getAdapter().addHeaderDrawerItems(new ContainerDrawerItem().withView(drawer.mHeaderView).withDivider(drawer.mHeaderDivider).withViewPosition(ContainerDrawerItem.Position.NONE));
        }
        //set the padding on the top to 0
        drawer.mRecyclerView.setPadding(drawer.mRecyclerView.getPaddingLeft(), 0, drawer.mRecyclerView.getPaddingRight(), drawer.mRecyclerView.getPaddingBottom());
    }
}
 
Example #17
Source File: ContainerDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
@Override
public void bindView(RecyclerView.ViewHolder holder) {
    Context ctx = holder.itemView.getContext();

    //get our viewHolder
    ViewHolder viewHolder = (ViewHolder) holder;

    //set the identifier from the drawerItem here. It can be used to run tests
    holder.itemView.setId(getIdentifier());

    //define how the divider should look like
    viewHolder.view.setEnabled(false);

    //make sure our view is not used in another parent
    if (mView.getParent() != null) {
        ((ViewGroup) mView.getParent()).removeView(mView);
    }

    //make sure the header view is empty
    ((ViewGroup) viewHolder.view).removeAllViews();

    int dividerHeight = 0;
    if (mDivider) {
        dividerHeight = 1;
    }

    View divider = new View(ctx);
    divider.setMinimumHeight(dividerHeight);
    divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_divider, R.color.material_drawer_divider));

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) UIUtils.convertDpToPixel(dividerHeight, ctx));

    //depending on the position we add the view
    if (mViewPosition == Position.TOP) {
        ((ViewGroup) viewHolder.view).addView(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.bottomMargin = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_padding);
        ((ViewGroup) viewHolder.view).addView(divider, layoutParams);
    } else if (mViewPosition == Position.BOTTOM) {
        layoutParams.topMargin = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_padding);
        ((ViewGroup) viewHolder.view).addView(divider, layoutParams);
        ((ViewGroup) viewHolder.view).addView(mView);
    } else {
        ((ViewGroup) viewHolder.view).addView(mView);
    }

    //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
    onPostBindView(this, holder.itemView);
}
 
Example #18
Source File: BasePrimaryDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * a helper method to have the logic for all secondaryDrawerItems only once
 *
 * @param viewHolder
 */
protected void bindViewHelper(BaseViewHolder viewHolder) {
    Context ctx = viewHolder.itemView.getContext();

    //set the identifier from the drawerItem here. It can be used to run tests
    viewHolder.itemView.setId(getIdentifier());

    //set the item selected if it is
    viewHolder.itemView.setSelected(isSelected());

    //
    viewHolder.itemView.setTag(this);

    //get the correct color for the background
    int selectedColor = getSelectedColor(ctx);
    //get the correct color for the text
    int color = getColor(ctx);
    int selectedTextColor = getSelectedTextColor(ctx);
    //get the correct color for the icon
    int iconColor = getIconColor(ctx);
    int selectedIconColor = getSelectedIconColor(ctx);

    //set the background for the item
    UIUtils.setBackground(viewHolder.view, DrawerUIUtils.getSelectableBackground(ctx, selectedColor));
    //set the text for the name
    StringHolder.applyTo(this.getName(), viewHolder.name);
    //set the text for the description or hide
    StringHolder.applyToOrHide(this.getDescription(), viewHolder.description);

    //set the colors for textViews
    viewHolder.name.setTextColor(getTextColorStateList(color, selectedTextColor));
    //set the description text color
    ColorHolder.applyToOr(getDescriptionTextColor(), viewHolder.description, getTextColorStateList(color, selectedTextColor));

    //define the typeface for our textViews
    if (getTypeface() != null) {
        viewHolder.name.setTypeface(getTypeface());
        viewHolder.description.setTypeface(getTypeface());
    }

    //get the drawables for our icon and set it
    Drawable icon = ImageHolder.decideIcon(getIcon(), ctx, iconColor, isIconTinted(), 1);
    Drawable selectedIcon = ImageHolder.decideIcon(getSelectedIcon(), ctx, selectedIconColor, isIconTinted(), 1);
    ImageHolder.applyMultiIconTo(icon, iconColor, selectedIcon, selectedIconColor, isIconTinted(), viewHolder.icon);

    //for android API 17 --> Padding not applied via xml
    DrawerUIUtils.setDrawerVerticalPadding(viewHolder.view, level);
}
 
Example #19
Source File: MiniDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
@Override
public void bindView(RecyclerView.ViewHolder holder) {
    Context ctx = holder.itemView.getContext();

    //get our viewHolder
    ViewHolder viewHolder = (ViewHolder) holder;

    //set the identifier from the drawerItem here. It can be used to run tests
    viewHolder.itemView.setId(getIdentifier());

    //set the item selected if it is
    viewHolder.itemView.setSelected(isSelected());

    //
    viewHolder.itemView.setTag(this);

    //get the correct color for the icon
    int iconColor = getIconColor(ctx);
    int selectedIconColor = getSelectedIconColor(ctx);

    if (mEnableSelectedBackground) {
        //get the correct color for the background
        int selectedColor = getSelectedColor(ctx);
        //set the background for the item
        UIUtils.setBackground(viewHolder.view, DrawerUIUtils.getSelectableBackground(ctx, selectedColor));
    }

    //set the text for the badge or hide
    boolean badgeVisible = StringHolder.applyToOrHide(mBadge, viewHolder.badge);
    //style the badge if it is visible
    if (badgeVisible) {
        mBadgeStyle.style(viewHolder.badge);
    }

    //get the drawables for our icon and set it
    Drawable icon = ImageHolder.decideIcon(getIcon(), ctx, iconColor, isIconTinted(), 1);
    Drawable selectedIcon = ImageHolder.decideIcon(getSelectedIcon(), ctx, selectedIconColor, isIconTinted(), 1);
    ImageHolder.applyMultiIconTo(icon, iconColor, selectedIcon, selectedIconColor, isIconTinted(), viewHolder.icon);

    //for android API 17 --> Padding not applied via xml
    int verticalPadding = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_padding);
    int topBottomPadding = ctx.getResources().getDimensionPixelSize(R.dimen.material_mini_drawer_item_padding);
    viewHolder.itemView.setPadding(verticalPadding, topBottomPadding, verticalPadding, topBottomPadding);

    //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
    onPostBindView(this, holder.itemView);
}
 
Example #20
Source File: BaseSecondaryDrawerItem.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * a helper method to have the logic for all secondaryDrawerItems only once
 *
 * @param viewHolder
 */
protected void bindViewHelper(BaseViewHolder viewHolder) {
    Context ctx = viewHolder.itemView.getContext();

    //set the identifier from the drawerItem here. It can be used to run tests
    viewHolder.itemView.setId(getIdentifier());

    //set the item selected if it is
    viewHolder.itemView.setSelected(isSelected());

    //get the correct color for the background
    int selectedColor = getSelectedColor(ctx);
    //get the correct color for the text
    int color = getColor(ctx);
    int selectedTextColor = getSelectedTextColor(ctx);
    //get the correct color for the icon
    int iconColor = getIconColor(ctx);
    int selectedIconColor = getSelectedIconColor(ctx);

    //set the background for the item
    UIUtils.setBackground(viewHolder.view, DrawerUIUtils.getSelectableBackground(ctx, selectedColor));
    //set the text for the name
    StringHolder.applyTo(this.getName(), viewHolder.name);

    //set the text for the description or hide
    StringHolder.applyToOrHide(this.getDescription(), viewHolder.description);

    //set the colors for textViews
    viewHolder.name.setTextColor(getTextColorStateList(color, selectedTextColor));

    //set the description text color
    ColorHolder.applyToOr(getDescriptionTextColor(), viewHolder.description, getTextColorStateList(getColor(ctx), getSelectedColor(ctx)));

    //define the typeface for our textViews
    if (getTypeface() != null) {
        viewHolder.name.setTypeface(getTypeface());
        viewHolder.description.setTypeface(getTypeface());
    }

    //get the drawables for our icon and set it
    Drawable icon = ImageHolder.decideIcon(getIcon(), ctx, iconColor, isIconTinted(), 1);
    Drawable selectedIcon = ImageHolder.decideIcon(getSelectedIcon(), ctx, selectedIconColor, isIconTinted(), 1);
    ImageHolder.applyMultiIconTo(icon, iconColor, selectedIcon, selectedIconColor, isIconTinted(), viewHolder.icon);

    //for android API 17 --> Padding not applied via xml
    DrawerUIUtils.setDrawerVerticalPadding(viewHolder.view, level);
}
 
Example #21
Source File: DrawerBuilder.java    From MaterialDrawer-Xamarin with Apache License 2.0 4 votes vote down vote up
/**
 * Call this method to append a new DrawerBuilder to a existing Drawer.
 *
 * @param result the Drawer.Result of an existing Drawer
 * @return
 */
public Drawer append(@NonNull Drawer result) {
    if (mUsed) {
        throw new RuntimeException("you must not reuse a DrawerBuilder builder");
    }
    if (mDrawerGravity == null) {
        throw new RuntimeException("please set the gravity for the drawer");
    }

    //set that this builder was used. now you have to create a new one
    mUsed = true;
    mAppended = true;

    //get the drawer layout from the previous drawer
    mDrawerLayout = result.getDrawerLayout();

    // get the slider view
    mSliderLayout = (RelativeLayout) mActivity.getLayoutInflater().inflate(R.layout.material_drawer_slider, mDrawerLayout, false);
    mSliderLayout.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(mActivity, R.attr.material_drawer_background, R.color.material_drawer_background));
    // get the layout params
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mSliderLayout.getLayoutParams();
    // set the gravity of this drawerGravity
    params.gravity = mDrawerGravity;
    // if this is a drawer from the right, change the margins :D
    params = DrawerUtils.processDrawerLayoutParams(this, params);
    // set the new params
    mSliderLayout.setLayoutParams(params);
    //define id for the sliderLayout
    mSliderLayout.setId(R.id.material_drawer_slider_layout);
    // add the slider to the drawer
    mDrawerLayout.addView(mSliderLayout, 1);

    //create the content
    createContent();

    //create the result object
    Drawer appendedResult = new Drawer(this);

    //toggle selection list if we were previously on the account list
    if (mSavedInstance != null && mSavedInstance.getBoolean(Drawer.BUNDLE_DRAWER_CONTENT_SWITCHED_APPENDED, false)) {
        mAccountHeader.toggleSelectionList(mActivity);
    }

    //forget the reference to the activity
    mActivity = null;

    return appendedResult;
}
 
Example #22
Source File: DrawerUIUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 2 votes vote down vote up
/**
 * helper to calculate the optimal drawer width
 *
 * @param context
 * @return
 */
public static int getOptimalDrawerWidth(Context context) {
    int possibleMinDrawerWidth = DrawerUIUtils.getScreenWidth(context) - UIUtils.getActionBarHeight(context);
    int maxDrawerWidth = context.getResources().getDimensionPixelSize(R.dimen.material_drawer_width);
    return Math.min(possibleMinDrawerWidth, maxDrawerWidth);
}
 
Example #23
Source File: DrawerUIUtils.java    From MaterialDrawer-Xamarin with Apache License 2.0 2 votes vote down vote up
/**
 * helper to get the system default selectable background inclusive an active state
 *
 * @param ctx
 * @param selected_color
 * @return
 */
public static StateListDrawable getSelectableBackground(Context ctx, int selected_color) {
    StateListDrawable states = getDrawerItemBackground(selected_color);
    states.addState(new int[]{}, UIUtils.getCompatDrawable(ctx, getSelectableBackground(ctx)));
    return states;
}