I have implemented hamburger -> arrow button on ActionBar, just like here: http://material-design.storage.googleapis.com/publish/v_2/material_ext_publish/0B08MbvYZK1iNWG5ldFpTU3VDd1E/animation-DelightfulDetails-DelightfulDetails-WellCrafted_v01_large_xhdpi.webm
It works nice, however I would like to change the background of this button when it is being pressed. How can I change the background of ActionBarDrawerToggle?
Below is workaround that solves the problem, but there have to be some nicer way:
for(int i=0; i<( getActionBarToolbar()).getChildCount(); ++i) {
View nextChild = getActionBarToolbar().getChildAt(i);
if (nextChild instanceof ImageButton)
{
nextChild.setBackgroundResource(R.drawable.selectable_background_indigo);
}
}
I am just iterating and looking for this ImageButton on which hamburger icon is shown, but there have to be some nicer way.
Related
I need to find the red buttons on Activity and set orange background color to these buttons.
When I click on the button I set it to red:
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
When I click on another button, the red buttons should turn orange.
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
I do not know how to get the id of the colors
For me is unclear your question but I'll try to answer it.
Supposing buttons is a List<Button>, so what you can do is.
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
And when you are clicking the button use
button.setBackgroundResource(Color.Red);
but.setBackgroundColor(Color.RED);
use that
I'm trying to disable all clickable items in the app bar layout when the opaque background appears after clicking the floating action button. But I also need to make sure that all the floating action buttons are all still clickable. I'm thinking maybe I can disable all items in the app bar programmatically?
How to achieve this?
UPDATE
Code to set fab visibility and animation. When the Fabs displayed, the tabs and toolbar finally disabled and unclickable. But i want to make my Fabs still clickable. How can i do this? Please advice. Thank you!
public void fabVisibility(){
if (isOpen){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabClose);
textViewActivities.startAnimation(fabClose);
fabPost.startAnimation(fabClose);
textViewPosts.startAnimation(fabClose);
fabMedia.startAnimation(fabClose);
textViewMedia.startAnimation(fabClose);
fabPlus.startAnimation(fabRotateAntiClockwise);
fabActivity.setClickable(false);
fabPost.setClickable(false);
fabMedia.setClickable(false);
shadowView.setVisibility(View.INVISIBLE);
isOpen = false;
}else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabOpen);
textViewActivities.startAnimation(fabOpen);
fabPost.startAnimation(fabOpen);
textViewPosts.startAnimation(fabOpen);
fabMedia.startAnimation(fabOpen);
textViewMedia.startAnimation(fabOpen);
fabPlus.startAnimation(fabRotateClockwise);
fabPlus.setEnabled(true);
fabActivity.setClickable(true);
fabActivity.setEnabled(true);
fabPost.setClickable(true);
fabPost.setEnabled(true);
fabMedia.setClickable(true);
fabMedia.setEnabled(true);
shadowView.setVisibility(View.VISIBLE);
isOpen = true;
}
}
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To disable action bar (app bar) buttons on clicking FAB icon, you could set a flag, let's say DisableAppBarButton.
Now call invalidateOptionsMenu() which will trigger onCreateOptionsMenu and will regenerate your menu.
Modify your onCreateOptionsMenu to disable the buttons.
if (DisableAppBarButton) {
menu.someItem(R.id.yourItem).setEnabled(false);
} else {
menu.someItem(R.id.yourItem).setEnabled(true);
}
Two options:
1 - You can set a view group to be disabled by recursively setting all of its children disabled. Example:
public static setEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setEnabled(viewGroup.getChild(i), enabled);
}
}
}
Then in your code in response to the FAB showing or hiding:
ViewUtil.setEnabled(mAppBarLayout, true /* or false */);
2 - You can make the opaque background focusable and clickable to intercept clicks while it's overlayed over the app bar, while the FAB buttons that float above that background would still receive clicks.
Hope that helps!
in my app i need to change the image of a toolbar button programmatically, and for so I´m using:
public boolean onCreateOptionsMenu( Menu menu ) {
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
toolbarButton = menu.findItem(R.id.action_button);
return true;
}
toolbarButton.setIcon(R.drawable.ic_settings);
And it works perfectly, the problem is that it happens too fast, so i´d need to add an animation to the transition, but I don´t know how to add an animation to a toolbar button.
Could somebody help me please?
Thanks a lot!
Okay, so finally thanks to Victorldavila i managed to get it working, this is my final code:
View button = toolbar.findViewById(R.id.action_button);
//Remove icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_fade_out);
animation.setStartOffset(0);
button.startAnimation(animation);
}
//Add icon animation
if (button != null) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_slide_in_up);
animation.setStartOffset(0);
button.startAnimation(animation);
}
Maybe these answers can help you:
https://stackoverflow.com/a/39182291/2759449
https://stackoverflow.com/a/30787535/2759449
I`m have 3 button in layout. I wish that when click one button, the styles of the other 2 buttons was set to "default". In my example: background color.
Pressing button1 must change her style and resets the styles of other buttons.
The simplest solution is to set the style of each button, on each method call, but this is the simplest solution, if the layer is only 2-5 buttons, but if there will be 10-20?
In drawable/button_bg.xml i have selector with 2 state: default and state_selected.
Is it possible to reset (or set) all styles of all buttons in the layer (layout) instantly and without reloading the app?
Or, if the button is not pressed, assign the default style? Or something like layout.AllButtons.setDefaultStyle(true) (sorry for that)
When click on one button, need to change styles for other
As you see now state_selected not reset after new click on other button.
You can do it manually by iterating over layout and setting needed params like
private void unselectButtons(ViewGroup root, View except) {
int childCount = root.getChildCount()
for (int i=0; i < childCount; ++i) {
View child = root.getChildAt(i);
if (child instanceof ViewGroup) {
unselectButtons((ViewGroup)child,except);
} else if (child instanceof Button && child != except) {
child.setSelected(false);
}
}
}
I can disable system clicking sound on a button if I use android:soundEffectsEnabled="false" in its layout.
I want to disable the sound effect on an Action Bar item. This attribute seems to make no difference.
How can I disable clicking sounds when user selects a menu item?
I use this:
final Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
final View child = toolbar.getChildAt(2);
if (child instanceof ActionMenuView)
{
final ActionMenuView actionMenuView = ((ActionMenuView) child);
actionMenuView.getChildAt(actionMenuView.getChildCount() - 1).setSoundEffectsEnabled(false);
}
If have navigation drawer use toolbar.getChildAt(2); if not use toolbar.getChildAt(1);
Was able to get this working in Android 11 by disabling sound effects for the view (in this case, an ImageView) associated with the action bar item. Hopefully this helps someone...
View view = menuItem.getActionView();
if (view != null) {
view.setSoundEffectsEnabled(false);
}