public static void themeDrawerItem(Context ctx, View view, int selected_color, boolean animate) {
        boolean legacyStyle = getBooleanStyleable(ctx, R.styleable.MaterialDrawer_material_drawer_legacy_style, false);

        Drawable selected;
        Drawable unselected;

        if (legacyStyle) {
            // Material 1.0 styling
            selected = new ColorDrawable(selected_color);
            unselected = UIUtils.getSelectableBackground(ctx);
        } else {
            // Material 2.0 styling
            int cornerRadius = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_item_corner_radius);
            int paddingTopBottom = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_item_background_padding_top_bottom);
            int paddingStartEnd = ctx.getResources().getDimensionPixelSize(R.dimen.material_drawer_item_background_padding_start_end);

            // define normal selected background
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(selected_color);
            gradientDrawable.setCornerRadius(cornerRadius);
            selected = new InsetDrawable(gradientDrawable, paddingStartEnd, paddingTopBottom, paddingStartEnd, paddingTopBottom);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // define mask for ripple
                GradientDrawable gradientMask = new GradientDrawable();
                gradientMask.setColor(Color.BLACK);
                gradientMask.setCornerRadius(cornerRadius);
                Drawable mask = new InsetDrawable(gradientMask, paddingStartEnd, paddingTopBottom, paddingStartEnd, paddingTopBottom);

                unselected = new RippleDrawable(new ColorStateList(new int[][]{new int[]{}}, new int[]{UIUtils.getThemeColor(ctx, R.attr.colorControlHighlight)}), null, mask);
            } else {
                // define touch drawable
                GradientDrawable touchDrawable = new GradientDrawable();
                touchDrawable.setColor(UIUtils.getThemeColor(ctx, R.attr.colorControlHighlight));
                touchDrawable.setCornerRadius(cornerRadius);
                Drawable touchInsetDrawable = new InsetDrawable(touchDrawable, paddingStartEnd, paddingTopBottom, paddingStartEnd, paddingTopBottom);

                StateListDrawable unselectedStates = new StateListDrawable();
                //if possible and wanted we enable animating across states
                if (animate) {
                    int duration = ctx.getResources().getInteger(android.R.integer.config_shortAnimTime);
                    unselectedStates.setEnterFadeDuration(duration);
                    unselectedStates.setExitFadeDuration(duration);
                }
                unselectedStates.addState(new int[]{android.R.attr.state_pressed}, touchInsetDrawable);
                unselectedStates.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
                unselected = unselectedStates;
            }
        }

        StateListDrawable states = new StateListDrawable();

        //if possible and wanted we enable animating across states
        if (animate) {
            int duration = ctx.getResources().getInteger(android.R.integer.config_shortAnimTime);
            states.setEnterFadeDuration(duration);
            states.setExitFadeDuration(duration);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            states.addState(new int[]{android.R.attr.state_selected}, selected);
            states.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));

            ViewCompat.setBackground(view, states);
            view.setForeground(unselected);
        } else {
            states.addState(new int[]{android.R.attr.state_selected}, selected);
            states.addState(new int[]{}, unselected);

            ViewCompat.setBackground(view, states);
        }
    }
public static void themeDrawerItem ( Context ctx , View view , int selected_color , boolean animate ) { boolean legacyStyle = getBooleanStyleable ( ctx , R . styleable . MaterialDrawer_material_drawer_legacy_style , false ) ; Drawable selected ; Drawable unselected ; if ( legacyStyle ) { selected = new ColorDrawable ( selected_color ) ; unselected = UIUtils . getSelectableBackground ( ctx ) ; } else { int cornerRadius = ctx . getResources ( ) . getDimensionPixelSize ( R . dimen . material_drawer_item_corner_radius ) ; int paddingTopBottom = ctx . getResources ( ) . getDimensionPixelSize ( R . dimen . material_drawer_item_background_padding_top_bottom ) ; int paddingStartEnd = ctx . getResources ( ) . getDimensionPixelSize ( R . dimen . material_drawer_item_background_padding_start_end ) ; GradientDrawable gradientDrawable = new GradientDrawable ( ) ; gradientDrawable . setColor ( selected_color ) ; gradientDrawable . setCornerRadius ( cornerRadius ) ; selected = new InsetDrawable ( gradientDrawable , paddingStartEnd , paddingTopBottom , paddingStartEnd , paddingTopBottom ) ; if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . LOLLIPOP ) { GradientDrawable gradientMask = new GradientDrawable ( ) ; gradientMask . setColor ( Color . BLACK ) ; gradientMask . setCornerRadius ( cornerRadius ) ; Drawable mask = new InsetDrawable ( gradientMask , paddingStartEnd , paddingTopBottom , paddingStartEnd , paddingTopBottom ) ; unselected = new RippleDrawable ( new ColorStateList ( new int [ ] [ ] { new int [ ] { } } , new int [ ] { UIUtils . getThemeColor ( ctx , R . attr . colorControlHighlight ) } ) , null , mask ) ; } else { GradientDrawable touchDrawable = new GradientDrawable ( ) ; touchDrawable . setColor ( UIUtils . getThemeColor ( ctx , R . attr . colorControlHighlight ) ) ; touchDrawable . setCornerRadius ( cornerRadius ) ; Drawable touchInsetDrawable = new InsetDrawable ( touchDrawable , paddingStartEnd , paddingTopBottom , paddingStartEnd , paddingTopBottom ) ; StateListDrawable unselectedStates = new StateListDrawable ( ) ; if ( animate ) { int duration = ctx . getResources ( ) . getInteger ( android . R . integer . config_shortAnimTime ) ; unselectedStates . setEnterFadeDuration ( duration ) ; unselectedStates . setExitFadeDuration ( duration ) ; } unselectedStates . addState ( new int [ ] { android . R . attr . state_pressed } , touchInsetDrawable ) ; unselectedStates . addState ( new int [ ] { } , new ColorDrawable ( Color . TRANSPARENT ) ) ; unselected = unselectedStates ; } } StateListDrawable states = new StateListDrawable ( ) ; if ( animate ) { int duration = ctx . getResources ( ) . getInteger ( android . R . integer . config_shortAnimTime ) ; states . setEnterFadeDuration ( duration ) ; states . setExitFadeDuration ( duration ) ; } if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . M ) { states . addState ( new int [ ] { android . R . attr . state_selected } , selected ) ; states . addState ( new int [ ] { } , new ColorDrawable ( Color . TRANSPARENT ) ) ; ViewCompat . setBackground ( view , states ) ; view . setForeground ( unselected ) ; } else { states . addState ( new int [ ] { android . R . attr . state_selected } , selected ) ; states . addState ( new int [ ] { } , unselected ) ; ViewCompat . setBackground ( view , states ) ; } }
Util method to theme the drawer item view's background (and foreground if possible)

@param ctx            the context to use
@param view           the view to theme
@param selected_color the selected color to use
@param animate        true if we want to animate the StateListDrawable