Java – How to get the maximum vertical offset for a collapsed toolbar layout

How to get the maximum vertical offset for a collapsed toolbar layout… here is a solution to the problem.

How to get the maximum vertical offset for a collapsed toolbar layout

I

currently have an AppBarLayout with a CollapsingToolbarLayout in it, I added an OffSetChangeListener so I can rotate the drop-down arrow in my app bar when the action bar expands, and now everything works fine, except I need the maximum vertical offset to calculate the degree to rotate, it won’t be provided as a parameter in the listener. Here is my code :

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int slideOffset = verticalOffset*(-1);
        int slideOffsetMax = 750;

int angle = slideOffset * 180 / slideOffsetMax;
        dropdownArrow.setRotation(angle);

}

});

The problem is that the maximum vertical offset varies from device to device, but if anyone has another way to implement it, please feel free to let me know. ps: I set my max offset to 750 because I recorded the offset to find the max for my device, but on my tablet it’s different.

Solution

Use:

int totalScrollRange = appBarLayout.getTotalScrollRange();

Get the scroll range of AppBarLayout and calculate it.

Related Problems and Solutions