I would programmatically like to create a button as defined in the design guidelines here: https://material.io/design/components/buttons.html#outlined-button, looking like this:
In XML I'm able to do this, using this piece of layout xml:
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonGetStarted"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="@string/title_short_intro" />
What I'm looking for is an example that shows how to do this using Java code? I have tried the following:
MaterialButton testSignIn = new MaterialButton( new ContextThemeWrapper( this, R.style.Widget_MaterialComponents_Button_OutlinedButton));
String buttonText = "Sign-in & empty test account";
testSignIn.setText( buttonText );
But this does not result in the outline variant:
You can use below:
MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);
If you want to apply a Outlined button you can use the R.attr.materialButtonOutlinedStyle
attribute style in the constructor:
MaterialButton outlinedButton = new MaterialButton(context,null, R.attr.materialButtonOutlinedStyle);
outlinedButton.setText("....");
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