setElevation() not working on AppBarLayout

Viewed 1703

I have a AppBarLayout, in which I wish to programatically set the elevation to 0 in the onCreate() method. If I have an example onCreate() like this..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(_toolbar);
    _appBar.setElevation(0);
}

The AppBarLayout elevation does NOT change.

However if I add a delay, for say, 400milliseconds, the AppBarLayout elevation DOES change.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(_toolbar);
        Observable.just(_appBar)
        .delay(400, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<AppBarLayout>() {
            @Override
            public void accept(AppBarLayout appBarLayout) throws Exception {
                appBarLayout.setElevation(0);
            }
        });
}

Can anyone explain why this is happening? and how I can fix this without a delay. I have also tried using getSupportActionBar().setElevation(0); as well, and using API 24

1 Answers
Related