I' am setting up a new application and came across the fact that the materialButton's height does't match the size which i set. So i tried on the regular Button and as you guys can see in the screenshot below. The buttons have different height although they getting the same height as you can see in my code.
How can i get normal height with MaterialButton?
Thank you.
public class MainActivity extends AppCompatActivity {
MaterialButton materialButton;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setId(getGeneratedId());
setContentView(linearLayout);
int buttonWidth = getResources().getDimensionPixelSize(R.dimen.buttonWidth);
int buttonHeight = getResources().getDimensionPixelSize(R.dimen.buttonHeight);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(buttonWidth, buttonHeight);
materialButton = new MaterialButton(this);
materialButton.setId(getGeneratedId());
materialButton.setLayoutParams(layoutParams);
materialButton.setBackgroundColor(Color.BLUE);
materialButton.setText("MatrialB");
materialButton.setTextColor(Color.WHITE);
button = new Button(this);
button.setId(getGeneratedId());
button.setLayoutParams(layoutParams);
button.setBackgroundColor(Color.RED);
button.setText("Button");
button.setTextColor(Color.WHITE);
linearLayout.addView(materialButton);
linearLayout.addView(button);
}
Integer getGeneratedId() {
return ViewCompat.generateViewId();
}
}
A convenience class for creating a new Material button. This class supplies updated Material styles for the button in the constructor. The widget will display the correct default Material styles without the use of the style flag.
1 Answer. Show activity on this post. If you are using a MaterialComponents Theme there is no difference between <Button /> and <com. google.
MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.
I believe the space on top and bottom of the MaterialButton is coming from android:insetTop
/android:insetBottom
which is 6dp
for me with material components 1.0.0. I am not sure, how to set these programmatically, but it can be done easily in xml:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:insetBottom="0dp"
android:insetTop="0dp"/>
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