Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pin title in Toolbar inside CollapsingToolbarLayout

Here is my layout:

<android.support.design.widget.AppBarLayout

    <android.support.design.widget.CollapsingToolbarLayout
        ...

        <android.support.v7.widget.Toolbar
            ...
        />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

I want title stays in Toolbar, not in CollapsingToolbarLayout. So I changed my code from:

mCollapsingToolbar = ...
mCollapsingToolbar.setTitle(title);

to:

mTitleBar = ...
setSupportActionBar(mTitleBar);
getSupportActionBar().setTitle(title);

But the title is not visible. My device is Nexus 6 5.1.0

Thanks in advance.


Update 1: I have changed code to this, still not work :(

setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Update 2: Here is code snippet for those three views:

private void setUpAppBarLayout() {
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
    appBarLayout.setBackgroundColor(extractBackgroundColor());
}

private void setUpCollapsingToolbarLayout() {
    if (null == mCollapsingToolbar) {
        mCollapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    }
}

private void setUpToolbar(String title) {
    mToolbar = (Toolbar) findViewById(R.id.tb_main);
    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            supportFinishAfterTransition();
        }
    });
    mToolbar.inflateMenu(R.menu.menu_group_activity);
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (R.id.action_settings == item.getItemId()) {
                ...
                return true;
            } else {
                return false;
            }
        }
    });
    mToolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRecyclerView.smoothScrollToPosition(0);
        }
    });

    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle(title);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

After called setSupportActionBar(mToolbar), both title and menu are invisible.

like image 582
Alex.Li Avatar asked Jul 14 '15 09:07

Alex.Li


People also ask

What is Collapsing toolbar layout?

CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout .

What is contentScrim in Android?

app:contentScrim: Specifies drawable or Color value when scrolled sufficiently off screen. app:layout_collapseMode: Specifies how child views of collapsing toolbar layout move when layout is moving. Parallax- moves in parallax fashion, Pin-view placed in fixed position.

What is Coordinator layout in Android?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.


1 Answers

In order for the Toolbar title to work with the CollapsingToolbarLayout you need to set the ctl's title enabled to false

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitleEnabled(false);
like image 129
Daniel Persson Avatar answered Nov 15 '22 23:11

Daniel Persson