set Toolbar height programmatically Android
Asked Answered
T

3

18

I am trying to set the height of toolbar programmatically by this way:

toolbar.setLayoutParams(new Toolbar.LayoutParams(LayoutParams.MATCH_PARENT, 42));
toolbar.setMinimumHeight(42);

But it causes fatal exception with this log -> android.widget.relativelayout$layoutparams cannot be cast to android.support.v7.Toolbar$layoutparams

I also try this variant:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
params.height = 42;   
toolbar.setLayoutParams(params);
toolbar.setMinimumHeight(42);

It does not give an exception but also it does not work and toolbar gets it's default bigger height in stead of my defined. But of course setting the parameters in xml works, but I need to set the height in java too.

Tincher answered 14/9, 2015 at 20:25 Comment(0)
T
21

Try this -

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mToolbar.getLayoutParams();
layoutParams.height = 42;
mToolbar.setLayoutParams(layoutParams);

It works fine for me.

If above code still doesn't work, try adding this line at the end -

mToolbar.requestLayout();

The above line will force your Toolbar to remeasure everything. Your code might not be working because you are trying to change the height after layout is inflated. In that case you need to force your View to remeasure everything. Take a look at following methods for more info - requestLayout(), invalidate() and forceLayout(). While altering the LayoutParams on go, these three methods comes handy but with a great price. If your Layout is very heavy and have lots of child views in it, calling any of these method will affect the performance of your app specially on lower end devices. So make sure you use these methods really carefully.

And the crash you mentioned is happening because your Toolbar is a child view of your root view and your root view is RelativeLayout. Hope it helps.

Also please don't follow the answer by Silambarasan Poonguti as its bit ambiguous. LayoutParams casting is needed if you are trying to access child LayoutParams. Even View has its own LayoutParams, you have to cast it to parent or root LayoutParams for ex. if your Toolbar is a child view of RelativeLayout then casting LayoutParams to Toolbar.LayoutParams will crash the app and you have to cast the LayoutParams to RelativeLayout.LayoutParams

Treadwell answered 15/9, 2015 at 5:7 Comment(1)
You should not cast layoutParams to RelativeLayout.LayoutParams, that's unsafe cast + height is defined in ViewGroup.LayoutParams, so: ViewGroup.LayoutParams layoutParams = mToolbar.getLayoutParams();Procrustes
E
5

Try this...

Toolbar has own LayoutParams. you can't cast it to RelativeLayout.

    Toolbar.LayoutParams params = (Toolbar.LayoutParams)mToolbar.getLayoutParams();
    params.height = 42;
    mToolbar.setLayoutParams(params);

If you want to set height of toolbar at run time simply do like this,

    int expected_height = your value;
    mToolbar.setMinimumHeight(expected_height );
Elroyels answered 15/9, 2015 at 4:49 Comment(2)
Thank you for answer, I have tried that code too, but it gives the same exception. Also setMinimumHeight is not even as in case of small minimum height it is getting the default minimum height of toolbarTincher
This is not how LayoutParams works. Even View has its own LayoutParams, you have to cast it to parent or root LayoutParams for ex. if your Toolbar is a child view of RelativeLayout then casting LayoutParams to Toolbar.LayoutParams will crash the app and you have to cast the LayoutParams to RelativeLayout.LayoutParamsTreadwell
V
0

To avoid the error you need to use ViewGroup.LayoutParams, since android.support.v7.Toolbar works with it and returns it on mToolbar.getLayoutParams().

Vietnam answered 3/5, 2016 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.