4

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Akshay
  • 6,029
  • 7
  • 40
  • 59

4 Answers4

16

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));
pkc456
  • 8,350
  • 38
  • 53
  • 109
Shrini Jaiswal
  • 1,090
  • 12
  • 12
  • For transparent color make use of this: collapsingToolbar.setExpandedTitleColor(Color.TRANSPARENT); – Gopi Cg Aug 16 '16 at 04:55
  • 1
    This won't work as already noted here https://stackoverflow.com/questions/31662416/show-collapsingtoolbarlayout-title-only-when-collapsed – David Aug 30 '17 at 09:42
8

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;
        }
    }
});
dara
  • 763
  • 7
  • 14
3

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

elsennov
  • 875
  • 9
  • 14
  • what about kitkat and above version? – Bhaumik Belani Jun 16 '16 at 10:18
  • ok working fine... but i want one more thing and i think in which you would help me. I want the title marquee and it is working in jellybean but not above that. here is the code i used-> http://stackoverflow.com/questions/27097126/marquee-title-in-toolbar-actionbar-in-android-with-lollipop-sdk?answertab=oldest#tab-top – Bhaumik Belani Jun 17 '16 at 06:11
  • It doesnt work in Android 6.0 and above. The above answer works. – Ali Kazi Jul 28 '16 at 09:08
  • 1
    This also won't work as already noted here https://stackoverflow.com/questions/31662416/show-collapsingtoolbarlayout-title-only-when-collapsed – David Aug 30 '17 at 09:42
0

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.

ShahiM
  • 3,179
  • 1
  • 33
  • 58