Akshay
Akshay

Reputation: 6142

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...

Upvotes: 4

Views: 18110

Answers (4)

ShahiM
ShahiM

Reputation: 3268

There is one workaround that I found:

On the CollapsingToolbar, set this:

app:expandedTitleGravity="top"
app:expandedTitleTextAppearance="@style/PosterText"

and in your styles add:

<style name="PosterText" parent="TextAppearance.Booksy.Headline5">
    <item name="android:textColor">#00000000</item>
</style>

This will give you a much less janky experience as the expanded title is still at the top.

Another way you could achieve this is instead of setting expandedTitleGravity, you could set app:titleCollapseMode="fade", but this currently has an issue where it does not respect the expanded titles transparency and just turns it to the opaque color (black in the above case).

I will let you know if I manage to solve it.

Upvotes: 0

dara
dara

Reputation: 763

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;
        }
    }
});

Upvotes: 8

Shrini Jaiswal
Shrini Jaiswal

Reputation: 1090

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));

Upvotes: 16

elsennov
elsennov

Reputation: 905

i found the same problem today. I finally solved it by defining it on xml

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

And also, checking for pre-kitkat programmatically

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    mCollapsingToolbar.setExpandedTitleTextAppearance(R.style.TransparentText)
};

Where TransparentText style is

<style name="TransparentText" parent="@android:style/TextAppearance">
       <item name="android:textColor">#00000000</item>
</style>

I know this is super late, but i hope this can help the others :)

Upvotes: 3

Related Questions