I finally wrap the button by doing this code. The key is checking the layout of the parent. Since the parent is a framelayout container, I used the Framelayout.LayoutParams.
mainButton.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));