Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collapsingToolbarLayout set title only when collapsed

I am trying to set collapsingToolbarLayout title only when it is collapsed

For that I set :

app:expandedTitleTextAppearance="@android:color/transparent"

And it's working good.

But on 4.2.2 app crash with following Log

STACK_TRACE

java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:687)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:666)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:633)
    at android.support.design.widget.CollapsingTextHelper.ensureExpandedTexture(CollapsingTextHelper.java:405)
    at android.support.design.widget.CollapsingTextHelper.setInterpolatedTextSize(CollapsingTextHelper.java:382)
    at android.support.design.widget.CollapsingTextHelper.calculateOffsets(CollapsingTextHelper.java:227)
    at android.support.design.widget.CollapsingTextHelper.setExpansionFraction(CollapsingTextHelper.java:203)
    at android.support.design.widget.CollapsingToolbarLayout$OffsetUpdateListener.onOffsetChanged(CollapsingToolbarLayout.java:754)
    at android.support.design.widget.AppBarLayout$Behavior.dispatchOffsetUpdates(AppBarLayout.java:851)
    at android.support.design.widget.AppBarLayout$Behavior.setAppBarTopBottomOffset(AppBarLayout.java:834)
    at android.support.design.widget.AppBarLayout$Behavior.scroll(AppBarLayout.java:793)
    at android.support.design.widget.AppBarLayout$Behavior.onNestedScroll(AppBarLayout.java:644)
    at android.support.design.widget.AppBarLayout$Behavior.onNestedScroll(AppBarLayout.java:583)
    at android.support.design.widget.CoordinatorLayout.onNestedScroll(CoordinatorLayout.java:1428)
    at android.support.v4.view.eh.a(ViewParentCompat.java:97)
    at android.support.v4.view.ec.a(ViewParentCompat.java:330)
    at android.support.v4.view.bk.a(NestedScrollingChildHelper.java:162)
    at android.support.v7.widget.RecyclerView.dispatchNestedScroll(RecyclerView.java:8306)
    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1387)
    at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2209)

Please suggest me the correct way to implement it.

OR

Is there any Listener to detect whether collapsingToolbarLayout collapsed so I can Hide/Unhide ToolBar title...

OR

Is it possible to set collapsingToolbarLayout title aligned with the another view I want...

like image 836
Akshay Avatar asked Nov 28 '22 01:11

Akshay


2 Answers

Final CollapsingToolbarLayout is as follows:-

collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setTitle("Create Delivery Personnel");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.transperent));
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.rgb(0, 0, 0));
like image 90
Shrini Jaiswal Avatar answered Feb 12 '23 00:02

Shrini Jaiswal


You can add listener to AppBar with this you can listen wether collapsed or not.

final Toolbar tool = (Toolbar)findViewById(R.id.toolbar);
CollapsingToolbarLayout c = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.app_bar_layout);
tool.setTitle("");
setSupportActionBar(tool);
c.setTitleEnabled(false);

appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

    boolean isVisible = true;
    int scrollRange = -1;
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        if (scrollRange + verticalOffset == 0) {
           tool.setTitle("Title");
            isVisible = true;
        } else if(isVisible) {
            tool.setTitle("");
            isVisible = false;
        }
    }
});
like image 43
dara Avatar answered Feb 11 '23 23:02

dara