Android: Programmatically Collapse and expand the CollapsingToolbarLayout - android

I have ImageView and TabLayout(4 Tabs) inside CollapsingToolbarLayout, Now i want to collapse Appbar when clicking on Tabs(2,3,4) and for first tab it should work normally(as per scrolling). Is there a way to expand and collapse Appbar programmatically?
however i have seen solution, appBarLayout.setExpanded(false) collapses Appbar but again it is able to drag down. i want to prevent AppBar Expansion until Tab 1 is clicked?

Use mAppBarLayout.setExpanded(true) to expand Toolbar and use mAppBarLayout.setExpanded(false) to collapse Toolbar.
If you want to prevent CollapsingToolbarLayout expansion until
Tab 1 is clicked then you should use mAppBarLayout.setLayoutParams(params) programmatically to change
CollapsingToolbarLayout height.
Collapse: Use when Tabs(2,3,4) clicked
CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // COLLAPSED_HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);
Expand: Use when Tab 1 clicked
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // EXPANDED_HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);
Hope this will help you~

Related

CollapsingToolbarLayout disable draw down expansion

I have a CollapsingToolbar that I have conditionally disabled. When the user loads the view under that condition, it just looks like a normal ToolBar object.
The only weird thing is that if they drag down, such as in a pull to refresh style action, the CollapsingToolbar expands, despite my wishes and code to the contrary!
Here is what I have, and the commented out code reflects what I have also tried
appBar.setExpanded(false);
appBar.setActivated(false);
/*CollapsingToolbarLayout.LayoutParams p = (CollapsingToolbarLayout.LayoutParams)toolbar.getLayoutParams();
p.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PIN);
toolbar.setLayoutParams(p);
CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
appBarLayoutParams.setBehavior(null);
appBar.setLayoutParams(appBarLayoutParams);*/
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBar.getLayoutParams();
lp.height = (int) getResources().getDimension(R.dimen.app_bar_height);
I want to disable the drag down expansion, and I didn't see a way to do it. This activity contains a recyclerview and that is what users primarily interact with.
I was closely with method. Expand layout behavior controls AppBarLayout. So you need update expand methods of that widget. Based on AppBarLayout Doc.
AppBarLayout appBarLayout;
//....
public void setExpandToolbar (boolean isEpand) {
if (isExpand) {
appBarLayout.setExpanded(true,true);
}
else {
appBarLayout.setExpanded(false,true);
}
}
Let me know, about this issue. Because you can control expand via other solution. For example, by custom layout params.
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams)
toolbar.getLayoutParams();
p.setScrollFlags(0);
toolbar.setLayoutParams(p);
To prevent scrolling of RecyclerView or NestedScrollView from expanding or collapsing the CollapsingToolbarLayout.
// scrollView can be RecyclerView or NestedScrollView
ViewCompat.setNestedScrollingEnabled(scrollView, false)
https://code.luasoftware.com/tutorials/android/how-to-disable-or-lock-collapsingtoolbarlayout-collapse-or-expand/
In kotlin looks like this: yourRV.isNestedScrollingEnabled = false

How to remove layout anchor_property of fab in programmatically?

I have added layout_anchor property on fab in layout xml. After clicking a button the fab should position on end of the screen. To achieve that I am thinking to add gravity property. But how do i remove layout_anchor property programmatically.
layout.xml
<com.samsoft.sam.Fab
android:id="#+id/fab"
app:layout_anchor="#id/asd"
app:layout_anchorGravity="top|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The above is my fab code and it positions perfectly, now I need to replace layout anchor property with android:gravity="top|end" programmatically. Please help me.
I assume you mean you want the FloatingActionButton's layout_gravity to end up as top|end.
You can do this by getting the FloatingActionButton's CoordinatorLayout.LayoutParams, clearing the anchor's ID, setting the gravity field as needed, and then setting the LayoutParams back on the FloatingActionButton.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.setAnchorId(View.NO_ID);
lp.gravity = Gravity.TOP | GravityCompat.END;
fab.setLayoutParams(lp);
((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setAnchorId(View.NO_ID);
This is the code you are looking for. Happy coding :)

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

Configure CoordinatorLayout from code

I'm dynamically injecting a content view into the CoordinatorLayout and would now like to apply a layout_below property to the injected view so that it isn't hidden behind the AppBar.
Is there any way to do this at runtime from code instead of xml properties of the annotation?
Taking one step back and building the entire view in plain xml, I realized that layout_below is not the property I needed for my use case: placing the content view below the app bar. I did not make this clear in my question though, as I assumed layout_below would be the proper option for that.
In fact, to insert a non scrolling view into the CoordinatorLayout it should first be wrapped with a android.support.v4.widget.NestedScrollView. Then, to avoid its content being hidden behind the app bar, it is necessary to update its behavior to android.support.design.widget.AppBarLayout.ScrollingViewBehavior. Otherwise a default behavior is used which will hide behind the app bar.
val viewToInsert = getLayoutInflater.inflate( id, coordinatorWrapper, false )
val p = viewToInsert.getLayoutParams.asInstanceOf[Coordinator.LayoutParams]
p.setBehavior( new ScrollingViewBehavior )
coordinatorWrapper.addView( viewToInsert, 1, p )
You could try setting an anchor.
ContentView view = getContentView(); //your view
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.setAnchorId(R.id.app_bar_layout);
params.anchorGravity = Gravity.BOTTOM; //we will anchor to the bottom line of the appbar
params.gravity = Gravity.BOTTOM; //we want to be BELOW that line
view.setLayoutParams(params);

Android: CollapsingToolbarLayout and SearchView, text overlapping

I have little problem with overlapping CollapsingToolbarLayout title with SearchView text. When is CollapsingToolbarLayout expanded, there is no problem:
But when is collapsed, the text is overlapped:
How to fix it?
I tried the answer by Tomas, but it had a problem that as soon as the user scrolls, the appbar collapses again and the problem re-appears.
So I came up with another solution which is to make the collapsed title text transparent when the searchview is expanded. This works nicely and does not depend on or change the collapse/expand state of the appbar.
Simply this:
if (searchViewExpanding) {
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.TRANSPARENT);
} else {
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);
}
Of course, you'll need to handle setOnActionExpandListener of your search menu item to know when to call this.
The answer is now simple, expand CollapsingToolbarLayout when search button is clicked. Thanks to Tuấn Trần Anh and this code:
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, null, 0, 1, new int[2]);
more information is in this thread.
EDIT
Still not solve it, they have soleved another realted problem. With changing the text. The trick now is using the ControllableAppLayout to know when the bar is collapsed or expanded so then you just set and empty title setTitle("")
You can find my implementation here https://gist.github.com/skimarxall/863585dcd7abde8f4153
Issue:
https://code.google.com/p/android/issues/detail?id=178138

Categories

Resources