Sumit Shukla
Sumit Shukla

Reputation: 4494

Getting elevation bewteen Toolbar and Tablayout

I am trying to create a layout which includes toolbar and tablayout inside appbarlayout. It works fine but shows a line between toolbar and appbarlayout. I am setting toolbar background color dynamically like:

toolbar.setBackgroundColor(Color.parseColor("myColor")); tabLayout.setBackgroundColor(Color.parseColor("myColor");

I have tried setting app:elevation="0dp" on Appbar and Toolbar but it doesn't work.

Image

<androidx.coordinatorlayout.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">   

    <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                style="@style/MyTabStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabMode="scrollable" />

        </com.google.android.material.appbar.AppBarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

MyTabsStyle.xml:

<style name="MyTabStyle" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/colorWhite</item>
    <item name="tabIndicatorHeight">2.5dp</item>
    <item name="android:elevation">2dp</item>
    <item name="tabIconTint">@color/colorWhite</item>
    <item name="tabSelectedTextColor">@color/colorWhite</item>
    <item name="tabTextColor">@color/white_trans</item>
</style>

Styles.xml:

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

Upvotes: 1

Views: 979

Answers (5)

Chintan
Chintan

Reputation: 414

Here the modified code in below.

<androidx.coordinatorlayout.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">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:elevation="0dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            style="@style/MyTabStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="scrollable" />

    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Add app:elevation="0dp" in AppBarLayout to hide the shadow in toolbar.

Upvotes: 1

Lin Lin
Lin Lin

Reputation: 259

You can wrap Toolbar and TabLayout with AppBarLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            app:tabBackground="?attr/colorPrimary"
            android:layout_height="wrap_content"/>

    </com.google.android.material.appbar.AppBarLayout>

<LinearLayout/>

Hope it helps.

Upvotes: 1

Ankit
Ankit

Reputation: 1068

Try this

set elevation to 0dp in your MyTabsStyle.xml

MyTabsStyle.xml:

 <style name="MyTabStyle" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@color/colorWhite</item>
<item name="tabIndicatorHeight">2.5dp</item>
<item name="android:elevation">0dp</item>
<item name="tabIconTint">@color/colorWhite</item>
<item name="tabSelectedTextColor">@color/colorWhite</item>
<item name="tabTextColor">@color/white_trans</item></style>

Upvotes: 1

Aravinth Velusamy
Aravinth Velusamy

Reputation: 140

Try this

   <androidx.appcompat.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorAppBar"
            android:elevation="0dp">

Upvotes: 0

Naresh NK
Naresh NK

Reputation: 1066

In your mytheme.xml change update AppTheme.AppBarOverlay style as :

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" >
    <item name="elevation">0dp</item>
</style>

Done.

Upvotes: 0

Related Questions