params.getBehaviour() returns null value - android

I was playing with the new Android Design Library. The CollapsingToolbarLayout works perfectly. However, I am having trouble setting default state of toolbar as Collapsed.
I am trying to implement the solution shown here and here
I am calling following code in my onResume of Activity:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null)
{
Log.d("DEBUG", "Behaviour is Not Null ");
int[] consumed = new int[2];
behavior.onNestedPreScroll(coordinator, appBarLayout, null, 0, 1000,consumed);
// behavior.onNestedFling(coordinator, appBarLayout, null, 0, 10000, true);
}
else
Log.d("DEBUG", "Behaviour is Null " );
However, the behaviour returned by params is null.
My xml is code same as here, except i am not using drawer and CordinatorLayout is my root Layout.
EDIT: I earlier tried switching AppBarLayout.Behavior to AppBarLayout.ScrollingViewBehavior and setting layout_behavior for AppBarLayout to #string/appbar_scrolling_view_behavior, but it resulted in weird layout.
Robin's answer works nicely. To Complement that, the behavior can also set in xml using following tag in AppBarLayout:
app:layout_behavior="android.support.design.widget.AppBarLayout$Behavior"

You have to set a layout behavior before.
Just use following code at your onCreate method:
((CoordinatorLayout.LayoutParams) YOUR_LAYOUT.getLayoutParams()).setBehavior(new AppBarLayout.Behavior() {});

Related

Disable HideBottomViewOnScrollBehaviour

I currently have a bottom navigation bar inside a Coordinator layout, which I added a HideBottomViewOnScrollBehaviour to. Some screens require the navigation bar to be hidden, which I can achieve by calling the slideUp / slideDown methods from the behaviour object of BottomNavigationBar layout params.
The issue is, even if i'm hiding it programatically, you can reveal it by simply scrolling up again.
I didn't find any solutions, i was thinking there will be something like disabling the behaviour and enabling it on certain screens, but that's not a thing.
Any solutions?
Thanks!
Here are methods to disable/enable scrolling behaviour:
fun enableLayoutBehaviour() {
val params = navView?.layoutParams as CoordinatorLayout.LayoutParams
if (params.behavior == null) {
params.behavior = HideBottomViewOnScrollBehavior<View>()
}
navView?.let {
(params.behavior as HideBottomViewOnScrollBehavior).slideUp(it)
}
}
fun disableLayoutBehaviour() {
val params = navView?.layoutParams as CoordinatorLayout.LayoutParams
navView?.let {
(params.behavior as HideBottomViewOnScrollBehavior).slideDown(it)
}
params.behavior = null
}
You can also just replace NestedScrollView with regular ScrollView on dedicated tabs to disable bottom bar from scrolling.

instanceof comparing two layouts returns unexpected result

I'm creating an app, where after I've dragged'n'dropped a shadow the receiving fragment is supposed to create a RelativeLayout, at the absolute coordinates where the shadow was dropped.
My problem lies in the LayoutParams of the dynamically created RelativeLayout and it not responding as a subclass of MarginLayoutParams.
According to this: RelativeLayout.LayoutParams extends ViewGroup.MarginLayoutParams and should therefore be a subclass of the latter.
My Code:
RelativeLayout relLay = new RelativeLayout(getView().getContext());
if(relLay.getLayoutParams() instanceof ViewGroup.MarginLayoutParams){
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) relLay.getLayoutParams();
params.setMargins(Math.round(coordX), Math.round(coordY), 0, 0);
relLay.setLayoutParams(new ViewGroup.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT));
RelativeLayout fragView = (RelativeLayout) getView().findViewById(R.id.RelativeStoryFaceFragment);
relLay.addView(iView);
fragView.addView(relLay);
}
The instanceof operator returns false, where I would expect it to return true. instanceof is not supposed to be there, but I'm using it to avoid the ugly crashes that will result until I've figured out why my code is wrong.
I can't set the margins unless I cast but the cast will crash if the instanceof returns false.
I've tried to focus on the problematic code, but will gladly include further context if necessary.
I'm probably missing something embarassing, any takers?

How to lock CollapsingToolbarLayout from Android support library

I am using Activity class with (usually) one fragment as a content. In the Activity I use CollapsingToolbarLayout as some kind of header for some information and everything works fine. But in some cases (when some fragments are attached) I don't want to show that info, I don't want CollapsingToolbarLayout to open on scroll.
What I want to achieve is to lock CollapsingToolbarLayout, prevent it from opening from the fragment. I am collapsing it programmatically with appBarLayout.setExpanded(false, true);
I came up with a different method as setting the nested scrolling flag only works when dragging the NestedScrollView. The appbar can still be expanded by swiping on the bar itself.
I set this up as a static function in "Utils" class. Obviously the flags you set upon unlocking will depend on which ones are relevant for your use case.
This function assumes you are are starting with an expanded toolbar
public static void LockToolbar(boolean locked, final AppBarLayout appbar, final CollapsingToolbarLayout toolbar) {
if (locked) {
// We want to lock so add the listener and collapse the toolbar
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (toolbar.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(toolbar)) {
// Now fully expanded again so remove the listener
appbar.removeOnOffsetChangedListener(this);
} else {
// Fully collapsed so set the flags to lock the toolbar
AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
}
}
});
appbar.setExpanded(false, true);
} else {
// Unlock by restoring the flags and then expand
AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
appbar.setExpanded(true, true);
}
}
Well, I managed to solve it myself. The trick is to disable nested scrolling behaviour with ViewCompat.setNestedScrollingEnabled(recyclerView, expanded);
As I am using one fragment in the activity as a content view and putting it on the backstack I simply check when backstack has changed and which fragment is visibile. Note that I NestedScrollView in every fragment to trigger collapsible toolbar. This is my code:
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
NestedScrollView nestedScrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
int size = getSupportFragmentManager().getBackStackEntryCount();
if (size >= 1 && nestedScrollView != null) {
if (getSupportFragmentManager().getBackStackEntryAt(size - 1).getName().equals("SpotDetailsFragment")) {
Log.d(LOG_TAG, "Enabling collapsible toolbar.");
ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
} else {
Log.d(LOG_TAG, "Disabling collapsible toolbar.");
ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
}
}
}
});
This thread helped me a lot, where another possible solution is presented:
Need to disable expand on CollapsingToolbarLayout for certain fragments

CollapsingToolbarLayout expand programmatically animation duration

I'm using CollapsingToolbarLayout in my application in Android. My app's minimum requirement API is 9.
I need the collapsed toolbar to be expanded when the user clicks in the collapsed one, just like in latest Gmail Calendar's app. So I set an onClickListener and inside it I do the following:
public void onClick(View v) {
if(toolbarExpanded) {
mAppBar.setExpanded(false, true);
} else {
mAppBar.setExpanded(true, true);
}
toolbarExpanded = !toolbarExpanded;
}
Which is working quite well but my problem is that the animation that it's running is slow, meaning a bad user experience.
Is there anyway to change the duration or to define a custom animation for this?
Thank you in advance.
Note: this answer is based on android design library v25.0.0.
You can call the private method animateOffsetTo of the AppBarLayout.Behavior of your NestedScrollView with reflection. This method has a velocity parameter that has an impact on the animation duration.
private void expandAppBarLayoutWithVelocity(AppBarLayout.Behavior behavior, CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, float velocity) {
try {
//With reflection, we can call the private method of Behavior that expands the AppBarLayout with specified velocity
Method animateOffsetTo = AppBarLayout.Behavior.getClass().getDeclaredMethod("animateOffsetTo", CoordinatorLayout.class, AppBarLayout.class, int.class, float.class);
animateOffsetTo.setAccessible(true);
animateOffsetTo.invoke(behavior, coordinatorLayout, appBarLayout, 0, velocity);
} catch (Exception e) {
e.printStackTrace();
//If the reflection fails, we fall back to the public method setExpanded that expands the AppBarLayout with a fixed velocity
Log.e(TAG, "Failed to get animateOffsetTo method from AppBarLayout.Behavior through reflection. Falling back to setExpanded.");
appBarLayout.setExpanded(true, true);
}
}
To get the Behavior, you need to fetch it from the LayoutParams of your AppBarLayout.
AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = params.getBehavior();
To expand with animation use
AppBarLayout.setExpanded(true,true);
To Collapse with animation use
AppBarLayout.setExpanded(false,true);

Programmatically collapse or expand CollapsingToolbarLayout

Simple question, but I can't find an answer. How can I collapse or expand the CollapsingToolbarLayout programmatically?
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Using Support Library v23, you can call appBarLayout.setExpanded(true/false).
Further reading: AppBarLayout.setExpanded(boolean)
I use this code for collapsing toolbar. Still cannot find a way to expand it.
public void collapseToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
}
}
Edit 1: The same function with negative velocityY but the toolbar is not expanded 100% and false for last param should work
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, -10000, false);
}
}
Edit 2: This code do the trick for me
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(rootLayout, appbarLayout, null, 0, 1, new int[2]);
}
}
setTopAndBottomOffset do expand the toolbar
onNestedPreScroll do show the content inside expanded toolbar
Will try to implement Behavior by myself.
You can define how much it expands or collapses with your custom animator.
Just use the setTopAndBottomOffset(int).
Here is an example:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
ValueAnimator valueAnimator = ValueAnimator.ofInt();
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
appBar.requestLayout();
}
});
valueAnimator.setIntValues(0, -900);
valueAnimator.setDuration(400);
valueAnimator.start();
}
I've written a small extension to AppBarLayout. It allows for expanding and collapsing of the CollapsibleToolbarLayout both with and without animation. It seems to be doing it quite right.
Feel free to try it out.
Just use it instead of your AppBarLayout, and you can call methods responsible for expanding or collapsing of the CollapsingToolbarLayout.
It's working exactly as expected in my project, but you might need to tweak the fling/scroll values in the perform... methods (especially in performExpandingWithAnimation()) to fit perfectly with your CollapsibleToolbarLayout.
Use mAppBarLayout.setExpanded(true) to expand Toolbar and use mAppBarLayout.setExpanded(false) to collapse Toolbar.
If you want to change CollapsingToolbarLayout height programmatically then just use mAppBarLayout.setLayoutParams(params);
Expand:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);
Collapse:
CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);
for the ones who wants to work with onNestedPreScroll and get error like me.
i get NullPointerException in onCreate with out this line
CoordinatorLayout coordinator =(CoordinatorLayout)findViewById(R.id.tab_maincontent);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
//below line
params.setBehavior(new AppBarLayout.Behavior() {});
and doesn't work properly with this.
but i work around this problem with
in onCreate :
scrollToolbarOnDelay();
and...
public void scrollToolbarOnDelay() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.tab_appbar);
CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.tab_maincontent);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null)
behavior.onNestedPreScroll(coordinator, appBarLayout, null, 0, 100, new int[]{0, 0});
else
scrollToolbarOnDelay()
}
}, 100);
}
Try this...
Expand
appBarLayout.setExpanded(true, true);
To recall
appBarLayout.setExpanded(false, true);
To expand/collapse AppBarLayout programmatically:
fun expandAppBarLayout(expand: Boolean, isAnimationEnabled: Boolean){
appBarLayout.setExpanded(expand, isAnimationEnabled);
}
This may help to expand or collapse :
appBarLayout.setActivated(true);
appBarLayout.setExpanded(true, true);
i have using this
private fun collapseAppbar() {
scrollView.postDelayed(Runnable {
scrollView?.smoothScrollTo(50, 50)
}, 400)
}

Categories

Resources