Android animate toolbar icon while changing its image - android

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

Related

How to disable all clickable items in AppBarLayout?

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!

Disable the navigation arrow in caldroid

I want to disable the navigation arrow in cal droid or else i want to reduce the size of navigation arrow in caldroid. Anyone give solution for this problem. If i give the
dialogCaldroidFragment.setShowNavigationArrows(false);
means the navigation arrow does not hide.
This is work for me
#Override
public void onCaldroidViewCreated() {
if (caldroidFragment.getLeftArrowButton() != null) {
caldroidFragment.setShowNavigationArrows(false);
}
}

Android L, ripple effect not working properly in Gridview

I'm using sdk 21 and added buttons to a gridview. However, somehow, only the first button is showing ripple effect when clicked and others not showing...
Edit: I found if I didn't add onClick listener to the buttons, the ripple effect is showing, but after I add the listener, only the first one is showing ripple effect...
Edit: Well, after further investigation, I found the effect is gone after I call setCompoundDrawablesWithIntrinsicBounds()
I got the resourceId by following:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.hashtag_button);
mSelectedResourceId = a.getResourceId(R.styleable.hashtag_button_selected_image, R.drawable.star_selected);
mUnselectedResourceId = a.getResourceId(R.styleable.hashtag_button_unselected_image, R.drawable.star_unselected);
a.recycle();
I change image by following method:
public void setFavorite(boolean isFavorite) {
mIsFavorite = isFavorite;
if (mIsFavorite) { //Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mSelectedResourceId, 0);
} else { //Not Favorite
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, mUnselectedResourceId, 0);
}
}
So this caused only first button has ripple effect... Not sure how to solve this...
Anyone has same issue?
Thanks

Changing the background of hamburger -> arrow button

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.

Android lollipop Menu button to Back button rendering

If you see the Google Play app , when swiping the menu the menu button convert itself to back button with the finger gradually and nicely, it seems not an animation. can any one help me to achieve this thing ?
Feel free to move/close the post but please be nice to redirect me in any helpful direction.
Icon transitions can be implemented using AnimatedStateListDrawable (There’s also support for animated vector icons.)
e.g.
public void setOrAnimatePlusCheckIcon(final ImageView imageView, boolean isCheck,
boolean allowAnimate) {
if (!hasL()) {
compatSetOrAnimatePlusCheckIcon(imageView, isCheck, allowAnimate);
return;
}
Drawable drawable = imageView.getDrawable();
if (!(drawable instanceof AnimatedStateListDrawable)) {
drawable = mActivity.getResources().getDrawable(R.drawable.add_schedule_fab_icon_anim);
imageView.setImageDrawable(drawable);
}
imageView.setColorFilter(isCheck ?
mActivity.getResources().getColor(R.color.theme_accent_1) : Color.WHITE);
if (allowAnimate) {
imageView.setImageState(isCheck ? STATE_UNCHECKED : STATE_CHECKED, false);
drawable.jumpToCurrentState();
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
} else {
imageView.setImageState(isCheck ? STATE_CHECKED : STATE_UNCHECKED, false);
drawable.jumpToCurrentState();
}
}
code from https://github.com/google/iosched

Categories

Resources