How to prevent collapsed toolbar from appearing behind the status bar? - android

I have the following:
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
app:contentScrim="?android:colorBackground"
app:title="ctl title"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:titleEnabled="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/materialToolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"
app:title="title" />
I use
WindowCompat.setDecorFitsSystemWindows(window, false)
to draw behind the navigation bar and the status bar.
But this also draws the collapsed toolbar behind the status bar like this:
How can I prevent that?

Related

Collapsing toolbar title gravity does not set to bottom

i am using collapsing toolbar with toolbar only here my purpose
is to use default parallex feature for toolbar,but the problem i am facing that back arrow and title in expanded form overlapping each other i tried to set expandedtitlegravity to bottom didn't work either
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="#style/SavedCollapsedAppBar"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="20dp"
app:expandedTitleMarginStart="20dp"
app:expandedTitleTextAppearance="#style/SavedExpandedAppBar"
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
android:fontFamily="sans-serif-medium"
android:theme="#style/SavedToolbarColoredBackArrow"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme"
app:titleTextAppearance="#style/SavedCollapsedAppBar"
app:titleTextColor="#color/black" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
It's seems like, the problem is with the Toolbar.
set app:contentInsetStart in Toolbar i.e.
<android.support.v7.widget.Toolbar
...
app:contentInsetStart="72dp"
It may solve the problem.

Collapsing Toolbar Layout with Custom Toolbar

I've been experimenting with CollapsingToolbarLayout in Android. I am trying to change the shape of the Toolbar when it's collapsed. And like this when collapsed.
I want the Toolbar to look something like this when collapsed. I can do it to a normal Toolbar when it's not in a CollapsingToolbarLayout by creating a custom background for it as shown above. But I'm not able to set it to the toolbar in CollapsingToolbarLayout as setting a background simply leads it to showing up in the expanded view and then shifting upwards when in collapsed form.
Any idea how this can be implemented?
XML Code for the CollapsingToolbar
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:titleEnabled="true"
app:title="LOL1"
app:expandedTitleGravity="center_horizontal"
app:collapsedTitleGravity="center_horizontal"
app:collapsedTitleTextAppearance="#color/colorAccent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:id="#+id/image"
android:src="#drawable/head"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
[![see image][3]][3]
[3]:

Change status bar scrim color with collapsing toolbar layout

I`m using android palette API to extract colors from the downloaded images by picasso, and accordingly i wanna change status bar and collapsing toolbar color.
i managed to change the collapsing toolbar but i cant with the status bar!
and this is my code
collapsingToolbar.setBackgroundColor(palette.getLightVibrantColor(00000)); // 00000 just for testing!
collapsingToolbar.setContentScrimColor(palette.getLightVibrantColor(00000));
collapsingToolbar.setStatusBarScrimColor(palette.getLightVibrantColor(00000));
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="#android:color/transparent">
<ImageView
android:id="#+id/back_drop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:id="#+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
use this method
private void applyPalette(Palette palette) {
int primaryDark = getResources().getColor(R.color.primary_dark);
int primary = getResources().getColor(R.color.primary);
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(primary));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getDarkMutedColor(primaryDark));
supportStartPostponedEnterTransition();
}

How to reduce left and bottom space from toolbar title in collapsing action bar android?

I need to reduce left and bottom space from toolbar title in collapsing action bar. I need to do something like whats app. Here is my code.
Thanks for help.
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/img_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/speaker1"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
add this below in your activity where you are setting the title
mBinding.collapseToolbar.setExpandedTitleMarginBottom((int) getResources().getDimension(R.dimen.contact_info_expanded_title_margin));
mBinding.collapseToolbar.setExpandedTitleMarginStart((int) getResources().getDimension(R.dimen.contact_info_expanded_title_margin));
mBinding.collapseToolbar.setTitle("Toolbar title");

How to remove transparent Toolbar padding when collapsed in CollapsingToolbarLayout

I use CollapsingToolbarLayout in my app but when it was collapsed, there is a marginLeft and marginRight in toolbar like picture below.
When I set the toolbar background not transparent ,such as red,it look as normal.
So how should I remove the margin?
I want it be this below but background to be transparent
my xml code is
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleGravity="center_horizontal"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:expandedTitleGravity="center_horizontal"
app:statusBarScrim="#color/transparent"
app:contentScrim="#color/transparent"
app:expandedTitleTextAppearance="#style/CollapsingToolbarLayoutExpandedText"
app:collapsedTitleTextAppearance="#style/CollapsingToolbarLayoutCollapsedText"
app:expandedTitleMarginTop="0dp"
app:expandedTitleMarginBottom="20dp">
<View
android:id="#+id/venueImage"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/transparent"
android:layout_marginTop="#dimen/status_bar_height"
app:layout_collapseMode="pin"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Adding app:elevation="0dp" to the AppBarLayout widget fixed this for me.
Also in my layout I have AppBarLayout nested inside CoordinatorLayout whereas your's is the reverse.
You can also set android:stateListAnimator null in your AppBarLayout; something like
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stateListAnimator="#null"> // This works

Categories

Resources