Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialButton size difference to Button

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();
}

}

Screenshot

like image 277
Apache Avatar asked Jul 01 '19 18:07

Apache


People also ask

What is MaterialButton?

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.

What is the difference between Button and material button in Android Studio?

1 Answer. Show activity on this post. If you are using a MaterialComponents Theme there is no difference between <Button /> and <com. google.

Do not set the background MaterialButton manages its own background drawable?

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.


1 Answers

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"/>
like image 122
ferini Avatar answered Oct 15 '22 11:10

ferini