Sparrow318
Sparrow318

Reputation: 478

How to remove AppBar shadow after app start

I have drawer layout, where I need to remove AppBar shadow for particular page (fragment). For this I am using this code

fun setToolbarShadow(dropShadow: Boolean) {
    if (dropShadow) {
        ViewCompat.setElevation(appBar, Utils.dp2px(4, resources))
    } else {
        ViewCompat.setElevation(appBar, 0f)
    }
}

and it works well for others than the first fragment. I tried put it inside onCreate of activity, onStart, onCreateView and onViewCreated of fragment, but nothing works. How to set it properly after activity and fragment are created?

EDIT: I made simple hello world app with one activity to try it, here is the code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
tools:context="cz.svobodaf.myapplication.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@+id/top_nav_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        ViewCompat.setElevation(appBar, 0f)
    }

    override fun onResume() {
        super.onResume()
        ViewCompat.setElevation(appBar, 0f)
    }
}

This also doesn't work after start, but when I lock device and unlock again, onResume is called and suddenly it works. I need to know ehere to put the code to set elevation after app start.

Upvotes: 1

Views: 162

Answers (1)

Sparrow318
Sparrow318

Reputation: 478

I found solution by setting AppBar outlines to null.

appBar.outlineProvider = null

But I couldn't figure out why elevation doesn't work. It looks like elevation is set after all starting methods are called -.-

Upvotes: 2

Related Questions