Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi menu item colors #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import com.github.rubensousa.bottomsheetbuilder.adapter.BottomSheetAdapterBuilder;
import com.github.rubensousa.bottomsheetbuilder.adapter.BottomSheetItemClickListener;
import com.github.rubensousa.bottomsheetbuilder.util.BottomSheetBuilderUtils;


public class BottomSheetBuilder {
Expand Down Expand Up @@ -108,14 +109,20 @@ public BottomSheetBuilder setItemClickListener(BottomSheetItemClickListener list
}

public BottomSheetBuilder setMenu(@MenuRes int menu) {
mMenu = new MenuBuilder(mContext);
new SupportMenuInflater(mContext).inflate(menu, mMenu);
return setMenu(mMenu);
return setMenu(BottomSheetBuilderUtils.inflateMenu(mContext, menu), null);
}

public BottomSheetBuilder setMenu(Menu menu) {
return setMenu(menu, null);
}

public BottomSheetBuilder setMenu(Menu menu, @ColorInt int[] titleTextColors) {
return setMenu(menu, titleTextColors, null);
}

public BottomSheetBuilder setMenu(Menu menu, @ColorInt int[] titleTextColors, @ColorInt int[] tintColors) {
mMenu = menu;
mAdapterBuilder.setMenu(mMenu);
mAdapterBuilder.setMenu(mMenu, titleTextColors, tintColors);
return this;
}

Expand Down Expand Up @@ -154,7 +161,12 @@ public BottomSheetBuilder addItem(int id, String title, @DrawableRes int icon) {
}

public BottomSheetBuilder addItem(int id, String title, Drawable icon) {
mAdapterBuilder.addItem(id, title, icon, mItemTextColor, mItemBackground, mIconTintColor);
addItem(id, title, icon, mItemTextColor, mIconTintColor);
return this;
}

public BottomSheetBuilder addItem(int id, String title, Drawable icon, @ColorInt int itemTextColor, @ColorInt int itemTintColor) {
mAdapterBuilder.addItem(id, title, icon, itemTextColor, mItemBackground, itemTintColor);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -44,6 +45,8 @@ public class BottomSheetAdapterBuilder {
private int mMode;
private Menu mMenu;
private boolean mFromMenu;
private int[] titleTextColors;
private int[] tintColors;
private Context mContext;

public BottomSheetAdapterBuilder(Context context) {
Expand All @@ -56,6 +59,12 @@ public void setMenu(Menu menu) {
mFromMenu = true;
}

public void setMenu(Menu menu, @ColorInt int[] titleTextColors, @ColorInt int[] tintColors) {
setMenu(menu);
this.titleTextColors = titleTextColors;
this.tintColors = tintColors;
}

public void setMode(int mode) {
mMode = mode;
}
Expand All @@ -68,6 +77,7 @@ public void addDividerItem(int dividerBackground) {
mItems.add(new BottomSheetDivider(dividerBackground));
}

@SuppressLint("RestrictedApi")
public void addItem(int id, String title, Drawable icon, int itemTextColor,
int itemBackground, int tintColor) {
if (mMenu == null) {
Expand All @@ -78,14 +88,15 @@ public void addItem(int id, String title, Drawable icon, int itemTextColor,
mItems.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
}


@SuppressLint("InflateParams")
public View createView(int titleTextColor, int backgroundDrawable, int backgroundColor,
int dividerBackground, int itemTextColor, int itemBackground,
int tintColor, BottomSheetItemClickListener itemClickListener) {
int dividerBackground, int defaultItemTextColor, int itemBackground,
int defaultTintColor, BottomSheetItemClickListener itemClickListener) {

if (mFromMenu) {
mItems = createAdapterItems(dividerBackground, titleTextColor,
itemTextColor, itemBackground, tintColor);
defaultItemTextColor, itemBackground, defaultTintColor);
}

LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Expand Down Expand Up @@ -148,13 +159,14 @@ public List<BottomSheetItem> getItems() {
}

private List<BottomSheetItem> createAdapterItems(int dividerBackground, int titleTextColor,
int itemTextColor, int itemBackground,
int tintColor) {
int defaultItemTextColor, int itemBackground,
int defaultTintColor) {
List<BottomSheetItem> items = new ArrayList<>();
mTitles = 0;

boolean addedSubMenu = false;

int itemCount = 0;
for (int i = 0; i < mMenu.size(); i++) {
MenuItem item = mMenu.getItem(i);

Expand All @@ -179,18 +191,34 @@ private List<BottomSheetItem> createAdapterItems(int dividerBackground, int titl
for (int j = 0; j < subMenu.size(); j++) {
MenuItem subItem = subMenu.getItem(j);
if (subItem.isVisible()) {
items.add(new BottomSheetMenuItem(subItem, itemTextColor,
itemBackground, tintColor));
items.add(new BottomSheetMenuItem(subItem,
getMenuItemTextColor(itemCount, defaultItemTextColor),
itemBackground,
getMenuItemTintColor(itemCount, defaultTintColor)));
addedSubMenu = true;
itemCount++;
}
}
} else {
items.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
items.add(new BottomSheetMenuItem(item,
getMenuItemTextColor(itemCount, defaultItemTextColor),
itemBackground,
getMenuItemTintColor(itemCount, defaultTintColor)));
itemCount++;
}
}
}

return items;
}

private int getMenuItemTextColor(int position, int defaultItemTextColor) {
return (titleTextColors != null && position < titleTextColors.length && titleTextColors[position] != -1) ?
titleTextColors[position] : defaultItemTextColor;
}

private int getMenuItemTintColor(int position, int defaultItemTintColor) {
return (tintColors != null && position < tintColors.length && tintColors[position] != -1) ?
tintColors[position] : defaultItemTintColor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

package com.github.rubensousa.bottomsheetbuilder.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.MenuRes;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.view.SupportMenuInflater;
import android.support.v7.view.menu.MenuBuilder;
import android.view.Menu;

public class BottomSheetBuilderUtils {

Expand Down Expand Up @@ -55,4 +61,11 @@ public void run() {
}, 300);
}
}

@SuppressLint("RestrictedApi")
public static Menu inflateMenu(Context context, @MenuRes int menu) {
Menu mMenu = new MenuBuilder(context);
new SupportMenuInflater(context).inflate(menu, mMenu);
return mMenu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ public class MainActivity extends AppCompatActivity implements BottomSheetItemCl
private boolean mShowingHeaderDialog;
private boolean mShowingGridDialog;
private boolean mShowingLongDialog;
private int colorAccent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
colorAccent = ContextCompat.getColor(this, R.color.colorAccent);

View bottomSheet = new BottomSheetBuilder(this, coordinatorLayout)
.setMode(BottomSheetBuilder.MODE_GRID)
Expand Down Expand Up @@ -137,7 +139,7 @@ public void onShowDialogClick() {
.setMode(BottomSheetBuilder.MODE_LIST)
.setAppBarLayout(appBarLayout)
.addTitleItem("Custom title")
.addItem(0, "Preview", R.drawable.ic_preview_24dp)
.addItem(0, "Preview", ContextCompat.getDrawable(this, R.drawable.ic_preview_24dp), colorAccent, colorAccent)
.addItem(1, "Share", R.drawable.ic_share_24dp)
.addDividerItem()
.addItem(2, "Get link", R.drawable.ic_link_24dp)
Expand Down Expand Up @@ -171,7 +173,8 @@ public void onShowDialogHeadersClick() {
mBottomSheetDialog = new BottomSheetBuilder(this, R.style.AppTheme_BottomSheetDialog_Custom)
.setMode(BottomSheetBuilder.MODE_LIST)
.setAppBarLayout(appBarLayout)
.setMenu(R.menu.menu_bottom_headers_sheet)
.setMenu(BottomSheetBuilderUtils.inflateMenu(this, R.menu.menu_bottom_headers_sheet),
new int[]{colorAccent, -1, -1, colorAccent}, new int[]{colorAccent, -1, -1, colorAccent})
.expandOnStart(true)
.setItemClickListener(new BottomSheetItemClickListener() {
@Override
Expand Down Expand Up @@ -254,4 +257,4 @@ public void onCancel(DialogInterface dialog) {
public void onBottomSheetItemClick(MenuItem item) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
}