Set toolbar height programmatically

I am trying to set the height of the toolbar programmatically as follows:

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

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

I also try this option:

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

It does not give an exception, but also does not work, and the toolbar gets by default a large height instead of my defined ones. But, of course, setting parameters in xml works, but I also need to set the height in java.

So please help me solve the problem.

+7
android android-layout android-xml android-toolbar android-viewgroup
source share
3 answers

Try it -

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

This works great for me.

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

 mToolbar.requestLayout(); 

The above line will make your toolbar still check. Perhaps your code does not work because you are trying to change the height after inflating the layout. In this case, you need to get your look to rethink everything. Take a look at the following methods for more information - requestLayout (), invalidate (), and forceLayout (). When changing LayoutParams on the go, these three methods work, but at a great price. If your layout is very heavy and has many child views, calling any of these methods will affect the performance of your application specifically on the lower end devices. Therefore, make sure that you use these methods very carefully.

And the crash you mentioned happens because your toolbar is a child of your root view and your root view is RelativeLayout. Hope this helps.

Also, please do not follow the answers of Silambarasan Punguti, as his bit is ambiguous. LayoutParams casting is necessary if you are trying to access LayoutParams layouts. Even View has its own LayoutParams, you need to give it to the parent or root LayoutParams for ex. if your toolbar is a child of RelativeLayout, then cast LayoutParams to the toolbar. LayoutParams will crash the application and you need to drop LayoutParams in RelativeLayout.LayoutParams

+10
source share

Try it...

The toolbar has LayoutParams . you cannot give it to RelativeLayout.

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

If you want to set the height of the toolbar at runtime, just do it,

  int expected_height = your value; mToolbar.setMinimumHeight(expected_height ); 

Happy coding ...

+1
source share

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

0
source share

All Articles