下面列出了怎么用android.support.v7.widget.ActionMenuView的API类实例代码及写法,或者点击链接到github查看源代码。
private void listing13_13() {
// Listing 13-13: Adding a menu to an Action Menu View
ActionMenuView actionMenuView = findViewById(R.id.menu_view);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.action_menu, actionMenuView.getMenu());
actionMenuView.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case (R.id.action_menu_item) :
// TODO Handle menu clicks.
return true;
default: return false;
}
}
});
}
/**
* It's important to set overflowDescription atribute in styles, so we can grab the reference
* to the overflow icon. Check: res/values/styles.xml
*
* @param activity
* @param colorFilter
*/
private static void setOverflowButtonColor(final Activity activity, final PorterDuffColorFilter colorFilter) {
final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final ArrayList<View> outViews = new ArrayList<View>();
decorView.findViewsWithText(outViews, overflowDescription,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty()) {
return;
}
final ActionMenuView overflowViewParent = (ActionMenuView) outViews.get(0).getParent();
overflowViewParent.getOverflowIcon().setColorFilter(colorFilter);
removeOnGlobalLayoutListener(decorView, this);
}
});
}
private void onGlobalLayout() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mToolbar.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
else
mToolbar.getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
ActionMenuView menuView = getMenuView();
for(int i = 0, count = menuView == null ? 0 : menuView.getChildCount(); i < count; i++){
View child = menuView.getChildAt(i);
if(mRippleStyle != 0){
if(child.getBackground() == null || !(child.getBackground() instanceof ToolbarRippleDrawable))
ViewUtil.setBackground(child, getBackground());
}
}
if(mGroupChanged){
animateIn();
mGroupChanged = false;
}
}
@Override
public void draw(@NonNull View view, @NonNull Object value) {
Toolbar toolbar = (Toolbar) view;
int themeId = (int) value;
try {
ActionMenuView actionMenuView = (ActionMenuView) sActionMenuViewField.get(toolbar);
if ( actionMenuView == null ){
toolbar.getContext().setTheme(themeId);
} else {
MenuPresenter presenter = (MenuPresenter) sPresenterField.get(actionMenuView);
Context context = (Context) sContextField.get(presenter);
context.setTheme(themeId);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
toolbar.setPopupTheme((Integer) value);
}
/**
* Get the list of visible MenuItems
*
* @param toolbar
* @return the list of visible MenuItems
*/
public static List<MenuItem> getVisibleMenuItemList(@NonNull Toolbar toolbar) {
List<MenuItem> list = new ArrayList<>();
for (int i = 0; i < toolbar.getChildCount(); i++) {
final View v = toolbar.getChildAt(i);
if (v instanceof ActionMenuView) {
int childCount = ((ActionMenuView) v).getChildCount();
for (int j = 0; j < childCount; j++) {
final View innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
list.add(((ActionMenuItemView) innerView).getItemData());
}
}
}
}
return list;
}
/**
* Search for a particular menu
*
* @param toolbar
* @param menuId
* @return the corresponding MenuItem, or null if not found
*/
public static MenuItem getMenuItem(@NonNull Toolbar toolbar, @IdRes int menuId) {
View v;
int childCount;
View innerView;
MenuItem menuItem;
for (int i = 0; i < toolbar.getChildCount(); i++) {
v = toolbar.getChildAt(i);
if (v instanceof ActionMenuView) {
childCount = ((ActionMenuView) v).getChildCount();
for (int j = 0; j < childCount; j++) {
innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
menuItem = ((ActionMenuItemView) innerView).getItemData();
if (menuItem.getItemId() == menuId) {
return menuItem;
}
}
}
}
}
return null;
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
private void animateIn(){
ActionMenuView menuView = getMenuView();
for(int i = 0, count = menuView == null ? 0 : menuView.getChildCount(); i < count; i++){
View child = menuView.getChildAt(i);
Animation anim = mAnimator.getInAnimation(child, i);
if(anim != null)
child.startAnimation(anim);
}
}
public FloatingSearchView(Context context, AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (isInEditMode()) {
mActivity = null;
} else {
mActivity = getActivity();
}
setFocusable(true);
setFocusableInTouchMode(true);
inflate(getContext(), R.layout.fsv_floating_search_layout, this);
mSearchInput = (LogoEditText)findViewById(R.id.fsv_search_text);
mNavButtonView = (ImageView) findViewById(R.id.fsv_search_action_navigation);
mRecyclerView = (RecyclerView) findViewById(R.id.fsv_suggestions_list);
mDivider = findViewById(R.id.fsv_suggestions_divider);
mSearchContainer = (ViewGroup) findViewById(R.id.fsv_search_container);
mActionMenu = (ActionMenuView) findViewById(R.id.fsv_search_action_menu);
//TODO: move elevation parameters to XML attributes
mSearchBackground = new RoundRectDrawableWithShadow(
DEFAULT_CONTENT_COLOR, ViewUtils.dpToPx(DEFAULT_RADIUS),
ViewUtils.dpToPx(DEFAULT_ELEVATION),
ViewUtils.dpToPx(DEFAULT_MAX_ELEVATION));
mSearchBackground.setAddPaddingForCorners(true);
mCardDecorator = new SuggestionItemDecorator(mSearchBackground.mutate());
applyXmlAttributes(attrs, defStyleAttr, 0);
setupViews();
}
public static void applyOverflowMenuTint(final @NonNull Context context, final Toolbar toolbar,
final @ColorInt int color) {
if (toolbar == null) {
return;
}
toolbar.post(new Runnable() {
@Override
public void run() {
try {
Field f1 = Toolbar.class.getDeclaredField("mMenuView");
f1.setAccessible(true);
ActionMenuView actionMenuView = (ActionMenuView) f1.get(toolbar);
Field f2 = ActionMenuView.class.getDeclaredField("mPresenter");
f2.setAccessible(true);
// Actually ActionMenuPresenter
BaseMenuPresenter presenter = (BaseMenuPresenter) f2.get(actionMenuView);
Field f3 = presenter.getClass().getDeclaredField("mOverflowPopup");
f3.setAccessible(true);
MenuPopupHelper overflowMenuPopupHelper = (MenuPopupHelper) f3.get(presenter);
setTintForMenuPopupHelper(context, overflowMenuPopupHelper, color);
Field f4 = presenter.getClass().getDeclaredField("mActionButtonPopup");
f4.setAccessible(true);
MenuPopupHelper subMenuPopupHelper = (MenuPopupHelper) f4.get(presenter);
setTintForMenuPopupHelper(context, subMenuPopupHelper, color);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public HeaderHolder(View v, MenuInflater menuInflater) {
super(v);
avatarImage = (ImageView) v.findViewById(R.id.avatar_image);
userNameText = (TextView) v.findViewById(R.id.user_name_text);
userCountText = (TextView) v.findViewById(R.id.user_count_text);
refreshMenu = (ActionMenuView) v.findViewById(R.id.refresh_menu);
menuInflater.inflate(R.menu.menu_refresh, refreshMenu.getMenu());
}
public FloatingSearchView(Context context, AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (isInEditMode()) {
mActivity = null;
} else {
mActivity = getActivity();
}
setFocusable(true);
setFocusableInTouchMode(true);
inflate(getContext(), R.layout.fsv_floating_search_layout, this);
mSearchInput = (LogoEditText)findViewById(R.id.fsv_search_text);
mNavButtonView = (ImageView) findViewById(R.id.fsv_search_action_navigation);
mRecyclerView = (RecyclerView) findViewById(R.id.fsv_suggestions_list);
mDivider = findViewById(R.id.fsv_suggestions_divider);
mSearchContainer = (ViewGroup) findViewById(R.id.fsv_search_container);
mActionMenu = (ActionMenuView) findViewById(R.id.fsv_search_action_menu);
//TODO: move elevation parameters to XML attributes
mSearchBackground = new RoundRectDrawableWithShadow(
DEFAULT_CONTENT_COLOR, ViewUtils.dpToPx(DEFAULT_RADIUS),
ViewUtils.dpToPx(DEFAULT_ELEVATION),
ViewUtils.dpToPx(DEFAULT_MAX_ELEVATION));
mSearchBackground.setAddPaddingForCorners(true);
mCardDecorator = new SuggestionItemDecorator(mSearchBackground.mutate());
applyXmlAttributes(attrs, defStyleAttr, 0);
setupViews();
}
public ActionMenuView getActionMenuView() {
return actionMenuView;
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
public ActionMenuView getActionMenuView() {
return actionMenuView;
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
public ActionMenuView getActionMenuView() {
return actionMenuView;
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
/**
* Use this method to colorize toolbar icons to the desired target color
*
* @param toolbarView toolbar view being colored
* @param toolbarIconsColor the target color of toolbar icons
* @param activity reference to activity needed to register observers
*/
public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
for (int i = 0; i < toolbarView.getChildCount(); i++) {
final View v = toolbarView.getChildAt(i);
//Step 1 : Changing the color of back button (or open drawer button).
if (v instanceof ImageButton) {
//Action Bar back button
((ImageButton) v).getDrawable().setColorFilter(colorFilter);
}
if (v instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) v).getChildCount(); j++) {
//Step 2: Changing the color of any ActionMenuViews - icons that are not back button, nor text, nor overflow menu icon.
//Colorize the ActionViews -> all icons that are NOT: back button | overflow menu
final View innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
for (int k = 0; k < ((ActionMenuItemView) innerView).getCompoundDrawables().length; k++) {
if (((ActionMenuItemView) innerView).getCompoundDrawables()[k] != null) {
final int finalK = k;
//Important to set the color filter in seperate thread, by adding it to the message queue
//Won't work otherwise.
innerView.post(new Runnable() {
@Override
public void run() {
((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
}
});
}
}
}
}
}
//Step 3: Changing the color of title and subtitle.
toolbarView.setTitleTextColor(toolbarIconsColor);
toolbarView.setSubtitleTextColor(toolbarIconsColor);
//Step 4: Changing the color of the Overflow Menu icon.
setOverflowButtonColor(activity, colorFilter);
}
}
/**
* Use this method to colorize toolbar icons to the desired target color
*
* @param toolbarView toolbar view being colored
* @param toolbarIconsColor the target color of toolbar icons
* @param activity reference to activity needed to register observers
*/
public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
for (int i = 0; i < toolbarView.getChildCount(); i++) {
final View v = toolbarView.getChildAt(i);
//Step 1 : Changing the color of back button (or open drawer button).
if (v instanceof ImageButton) {
//Action Bar back button
((ImageButton) v).getDrawable().setColorFilter(colorFilter);
}
if (v instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) v).getChildCount(); j++) {
//Step 2: Changing the color of any ActionMenuViews - icons that are not back button, nor text, nor overflow menu icon.
//Colorize the ActionViews -> all icons that are NOT: back button | overflow menu
final View innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
for (int k = 0; k < ((ActionMenuItemView) innerView).getCompoundDrawables().length; k++) {
if (((ActionMenuItemView) innerView).getCompoundDrawables()[k] != null) {
final int finalK = k;
//Important to set the color filter in seperate thread, by adding it to the message queue
//Won't work otherwise.
innerView.post(new Runnable() {
@Override
public void run() {
((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
}
});
}
}
}
}
}
//Step 3: Changing the color of title and subtitle.
toolbarView.setTitleTextColor(toolbarIconsColor);
toolbarView.setSubtitleTextColor(toolbarIconsColor);
//Step 4: Changing the color of the Overflow Menu icon.
setOverflowButtonColor(activity, colorFilter);
}
}
public void setOnMenuItemClickListener(ActionMenuView.OnMenuItemClickListener listener) {
mActionMenu.setOnMenuItemClickListener(listener);
}
@SuppressWarnings("unchecked")
public static void setToolbarContentColor(@NonNull Context context, Toolbar toolbar,
@Nullable Menu menu, final @ColorInt int toolbarContentColor,
final @ColorInt int titleTextColor, final @ColorInt int subtitleTextColor,
final @ColorInt int menuWidgetColor) {
if (toolbar == null) {
return;
}
if (menu == null) {
menu = toolbar.getMenu();
}
toolbar.setTitleTextColor(titleTextColor);
toolbar.setSubtitleTextColor(subtitleTextColor);
if (toolbar.getNavigationIcon() != null) {
// Tint the toolbar navigation icon (e.g. back, drawer, etc.)
toolbar.setNavigationIcon(
TintHelper.createTintedDrawable(toolbar.getNavigationIcon(), toolbarContentColor));
}
InternalToolbarContentTintUtil.tintMenu(toolbar, menu, toolbarContentColor);
InternalToolbarContentTintUtil.applyOverflowMenuTint(context, toolbar, menuWidgetColor);
if (context instanceof Activity) {
InternalToolbarContentTintUtil
.setOverflowButtonColor((Activity) context, toolbarContentColor);
}
try {
// Tint immediate overflow menu items
final Field menuField = Toolbar.class.getDeclaredField("mMenuBuilderCallback");
menuField.setAccessible(true);
final Field presenterField = Toolbar.class.getDeclaredField("mActionMenuPresenterCallback");
presenterField.setAccessible(true);
final Field menuViewField = Toolbar.class.getDeclaredField("mMenuView");
menuViewField.setAccessible(true);
final MenuPresenter.Callback currentPresenterCb = (MenuPresenter.Callback) presenterField
.get(toolbar);
if (!(currentPresenterCb instanceof ATHMenuPresenterCallback)) {
final ATHMenuPresenterCallback newPresenterCb = new ATHMenuPresenterCallback(context,
menuWidgetColor, currentPresenterCb, toolbar);
final MenuBuilder.Callback currentMenuCb = (MenuBuilder.Callback) menuField.get(toolbar);
toolbar.setMenuCallbacks(newPresenterCb, currentMenuCb);
ActionMenuView menuView = (ActionMenuView) menuViewField.get(toolbar);
if (menuView != null) {
menuView.setMenuCallbacks(newPresenterCb, currentMenuCb);
}
}
// OnMenuItemClickListener to tint submenu items
final Field menuItemClickListener = Toolbar.class
.getDeclaredField("mOnMenuItemClickListener");
menuItemClickListener.setAccessible(true);
Toolbar.OnMenuItemClickListener currentClickListener = (Toolbar.OnMenuItemClickListener) menuItemClickListener
.get(toolbar);
if (!(currentClickListener instanceof ATHOnMenuItemClickListener)) {
final ATHOnMenuItemClickListener newClickListener = new ATHOnMenuItemClickListener(context,
menuWidgetColor, currentClickListener, toolbar);
toolbar.setOnMenuItemClickListener(newClickListener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setOnMenuItemClickListener(ActionMenuView.OnMenuItemClickListener listener) {
mActionMenu.setOnMenuItemClickListener(listener);
}
public static void doColorizing(View v, final ColorFilter colorFilter, int toolbarIconsColor) {
if (v instanceof ImageButton) {
((ImageButton) v).getDrawable().setAlpha(255);
((ImageButton) v).getDrawable().setColorFilter(colorFilter);
}
if (v instanceof ImageView && !(v instanceof AvatarView)) {
((ImageView) v).getDrawable().setAlpha(255);
((ImageView) v).getDrawable().setColorFilter(colorFilter);
}
if (v instanceof AutoCompleteTextView) {
((AutoCompleteTextView) v).setTextColor(toolbarIconsColor);
}
if (v instanceof TextView) {
((TextView) v).setTextColor(toolbarIconsColor);
}
if (v instanceof EditText) {
((EditText) v).setTextColor(toolbarIconsColor);
}
if (v instanceof ViewGroup) {
for (int lli = 0; lli < ((ViewGroup) v).getChildCount(); lli++) {
doColorizing(((ViewGroup) v).getChildAt(lli), colorFilter, toolbarIconsColor);
}
}
if (v instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) v).getChildCount(); j++) {
//Step 2: Changing the color of any ActionMenuViews - icons that
//are not back button, nor text, nor overflow menu icon.
final View innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
int drawablesCount = ((ActionMenuItemView) innerView).getCompoundDrawables().length;
for (int k = 0; k < drawablesCount; k++) {
if (((ActionMenuItemView) innerView).getCompoundDrawables()[k] != null) {
final int finalK = k;
//Important to set the color filter in seperate thread,
//by adding it to the message queue
//Won't work otherwise.
//Works fine for my case but needs more testing
((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
// innerView.post(new Runnable() {
// @Override
// public void run() {
// ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
// }
// });
}
}
}
}
}
}
public static BaseDSL.ViewClassResult actionMenuView() {
return BaseDSL.v(ActionMenuView.class);
}
public static Void actionMenuView(Anvil.Renderable r) {
return BaseDSL.v(ActionMenuView.class, r);
}
public static Void onMenuItemClick(ActionMenuView.OnMenuItemClickListener arg) {
return BaseDSL.attr("onMenuItemClick", arg);
}
/**
* Use this method to colorize toolbar icons to the desired target color
*
* @param toolbarView toolbar view being colored
* @param toolbarIconsColor the target color of toolbar icons
* @param activity reference to activity needed to register observers
*/
public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
for (int i = 0; i < toolbarView.getChildCount(); i++) {
final View v = toolbarView.getChildAt(i);
//Step 1 : Changing the color of back button (or open drawer button).
if (v instanceof ImageButton) {
//Action Bar back button
((ImageButton) v).getDrawable().setColorFilter(colorFilter);
}
if (v instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) v).getChildCount(); j++) {
//Step 2: Changing the color of any ActionMenuViews - icons that are not back button, nor text, nor overflow menu icon.
//Colorize the ActionViews -> all icons that are NOT: back button | overflow menu
final View innerView = ((ActionMenuView) v).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
for (int k = 0; k < ((ActionMenuItemView) innerView).getCompoundDrawables().length; k++) {
if (((ActionMenuItemView) innerView).getCompoundDrawables()[k] != null) {
final int finalK = k;
//Important to set the color filter in seperate thread, by adding it to the message queue
//Won't work otherwise.
innerView.post(new Runnable() {
@Override
public void run() {
((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
}
});
}
}
}
}
}
//Step 3: Changing the color of title and subtitle.
toolbarView.setTitleTextColor(toolbarIconsColor);
toolbarView.setSubtitleTextColor(toolbarIconsColor);
//Step 4: Changing the color of the Overflow Menu icon.
setOverflowButtonColor(activity, colorFilter);
}
}