Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setElevation() not working on AppBarLayout

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

like image 425
Tmarsh2 Avatar asked Jul 09 '17 14:07

Tmarsh2


1 Answers

Solved! instead of set elevation, I use:

StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);

Please see the following answer for a more detailed explanation:

How to set the elevation of an AppBarLayout programmatically in the Android Support Library v24.0.0?

like image 113
Tmarsh2 Avatar answered Nov 10 '22 10:11

Tmarsh2