KotlinNoob
KotlinNoob

Reputation: 65

Android studio custom button style padding issues

After 4 months of being inactive i realized that there are some changes.

Now when you make a new button it is purple and has some padding defined. Managed to kill the purple color by editing the theme, but i still have issues with padding. When i make a new button, by default it has left and right padding 16dp, and top and bottom 4. I would not like to be forced to change every single new button to 0 padding..

i have 2 options in mind. To set padding in custom button xml:

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
    <corners android:radius="12dp" />
    <solid android:color="#000000" />
    
    <padding
    
    android:left="0dp"
    
    android:top="0dp"
    
    android:right="0dp"
    
    android:bottom="0dp" />
    
    </shape>

...but for some reason this doesnt work.

Can someone figure out what can be the issue?

Or to tell me how can i change default padding values for new buttons?

Also values for some insetBotton and insetTop to be set to 0 by default and not 6

Upvotes: 3

Views: 721

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363825

Using a Material Components theme the Button is replaced at runtime by a MaterialButton.

The MaterialButton has a default style with insetBottom and insetTop with a value of 6dp.

If you want to change globally the button style in your app you can also add the materialButtonStyle attribute in your app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
   <item name="materialButtonStyle">@style/Widget.App.Button</item>
</style>

<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="android:paddingLeft">....</item>
    <item name="android:paddingRight">...</item>
    <item name="android:insetTop">...</item>
    <item name="android:insetBottom">....</item>
</style>

Upvotes: 4

Ole Pannier
Ole Pannier

Reputation: 3673

Go to your res folder -> values -> styles Insert the following lines below to edit the standard button et voilà:

<style name="AppTheme" parent="Widget.AppCompat.Button">
    <item name="android:minWidth">0dp</item>
    <item name="android:minHeight">0dp</item>
</style>

That should work, give it a shot buddy.

Upvotes: 1

Related Questions