Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create a MaterialButton with Outline style

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:

enter image description here

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:

enter image description here

like image 621
Peter Avatar asked Aug 31 '18 17:08

Peter


2 Answers

You can use below:

MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);
like image 104
TheND Avatar answered Sep 21 '22 13:09

TheND


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("....");

enter image description here

like image 25
Gabriele Mariotti Avatar answered Sep 18 '22 13:09

Gabriele Mariotti