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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With