animateLayoutChanges not animate removing back arrow from Toolbar - android

I have CoordinatorLayout with AppBarLayout and Toolbar inside it:
CoordinatorLayout
- AppBarLayout
- Toolbar
animateLayoutChanges=true
Nice. And I use this code to hide/show back arrow button:
private void setBackArrowState(boolean state) {
actionBar.setDisplayHomeAsUpEnabled(state);
actionBar.setDisplayShowHomeEnabled(state);
}
There is what I got in result:
When the back arrow button hide, title not animated to it's normal position. How can I solve it?

Related

Collapsing toolbar anchored view is hidding when collapse

I need to display a TextView (The circular shape with "0") anchored to the toolbar, like a FAB, but it must be ALWAYS VISIBLE.
My problem is that when I collapse the toolbar (scrolling the recyclerView), at the fully collapsed state, it hides the half of the view...
The TextView layout has this properties:
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|center_horizontal"
Anyone knows how to solve it?
If you want your TextView be over the toolbar, in this case, you can try to set higher elevation value to TextView. In some cases, it worked for me.
For example:
android:elevation="15dp"

CollapsingToolbar doesn't fully collapse

I have a Navigation Drawer Activity (NavActivity) in which one of the fragments (OuterFrag.xml) has a Collapsing Toolbar and a View Pager. When I scroll in the View Pager(InnerFrag.xml), the toolbar collapses, but not fully. Here's the image and the code before I go any further..
OuterFrag.xml
InnerFrag.xml
The problem has to be in OuterFrag, which contains the CollapsingToolbar. I've tried setting the height of the CollapsingToolbar to "1dp", but then a black Toolbar appears with the same size of the red are in the image above!
Found the solution!
I removed the android:fitsSystemWindows="true" from the CoordinatorLayout and the AppBarLayout and now, the Toolbar collapses completely!
If you are using CollapsingToolbarLayout inside a fragment which is inside a fullscreen activity, removing all the fitSystemWindows="true" will make it work.

Change FAB position when anchor is gone

im using a coordinatorLayout and a floating action button between two layers, like this:
in the orange part i put a NestedScrollView to show alot of information about the product that have his photo on purple part.
When the users scrolls the layout and the anchor of the FAB ( the image on purple layout) is gone, the FAB keeps on top|right of the layout.
what im trying to do is: when the anchor is gone, the FAB go to buttom|right of the layout insted to remain on top of screen.
can you help me? thanks :)
I've used this code to change the actionbar title when to toolbar collapses. You can reuse it to change the fab position instead of changing the title :
((AppBarLayout) findViewById(R.id.appbar)).addOnOffsetChangedListener(new AppBarStateChangeListener() {
#Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
switch (state) {
case COLLAPSED:
getSupportActionBar().setTitle(mMatch.getUniversity().getDisplayName());
break;
case NOT_COLLAPSED:
getSupportActionBar().setTitle("");
break;
}
}
});

Hide/Show Toolbar programmatically on CoordinatorLayout

When I scroll my RecycleView ToolBar hide or show (with animation).
How I can return ToolBar back programmatically?
If your toolbar is inside an AppBarLayout which is probably inside your CoordinatorLayout then something like this should work.
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
appBarLayout.setExpanded(true, true);
Or to collapse it
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
appBarLayout.setExpanded(false, true);
Here is the definition
setExpanded(boolean expanded, boolean animate)
Take note that this method is available from v23 of the support library, here is some documentation for reference, the key thing to note is "As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout."
Is that what you looking for?
Toolbar toolbar = findViewById(R.id.toolbar); // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags
link: How to enable/disable toolbar scrolling programmatically when using design support library
In order to hide the Toolbar your can just do something like this:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
If you want to show it again you call:
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
My problem was very similar to #Artem I tried many fix but none of them worked for me. #Jraco11's answer is correct when you use AppBarLayout. #johnrao07 not worked for me. But I found a perfect solution for this problem when we use Toolbar.
To hide Toolbar programatically
if (toolbar.getParent() instanceof AppBarLayout){
((AppBarLayout)toolbar.getParent()).setExpanded(false,true);
}
To show Toolbar programatically
if (toolbar.getParent() instanceof AppBarLayout){
((AppBarLayout)toolbar.getParent()).setExpanded(true,true);
Refer original answer(answer by #Android HHT):- programmatically-show-toolbar-after-hidden-by-scrolling-android-design-library

How do I shift content up when I am animating Toolbar out?

I have a layout identical to the Play Store where I have a Toolbar, Tab Strip, and ViewPager all in a vertical LinearLayout. I want to achieve the quick return pattern of the Play Store where the Toolbar hides but the TabStrip and ViewPager stay but animate up with the Toolbar.
I have the animating Toolbar part down using animate().translateY() but I can't get the content to shift up with it (at least not smoothly). I've tried something like:
<FrameLayout>
<Toolbar (with WindowActionBarOverlay = true)>
<LinearLayout paddingTop = Toolbar_height>
*Contains all the stuff I don't want to hide*
</LinearLayout>
</FrameLayout>
But this doesn't make the content shift up either. So I tried setting the Top Padding of the LinearLayout to 0 after I animate the Toolbar but that is instantaneous rather than animating with the ToolBar. So I tried to animate the entire LinearLayout instead using animate().translateY() but that is a bit laggy and has some unwanted side effects.
Anyone have any ideas? For RecyclerView and preferably a minSDK of 15.
Try adding an animator listener on the toolbar's translation to update the padding. Back-of-napkin code:
toolbar.animate()
.translateY(-toolbar.getHeight())
.setUpdateListener(new AnimatorUpdateListener()) {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
contentView.setPadding(
contentView.getPaddingLeft(),
// The padding is the inverse of the animation progress.
toolbar.getHeight() * (1f - animation.getAnimatedFraction()),
contentView.getPaddingRight(),
contentView.getPaddingBottom());
}
});
I'd be interested to see what the performance is like updating the layout on each animation frame like that.

Categories

Resources