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
Varundroid
source share