How to Remove Padding Around Buttons in Android

How to remove padding around buttons in Android?

For me the problem turned out to be minHeight and minWidth on some of the Android themes.

On the Button element, add:

<Button android:minHeight="0dp" android:minWidth="0dp" ...

Or in your button's style:

<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>

How to properly remove padding (or margin?) around buttons in Android?

A standard button is not supposed to be used at full width which is why you experience this.

Background

If you have a look at the Material Design - Button Style you will see that a button has a 48dp height click area, but will be displayed as 36dp of height for...some reason.

This is the background outline you see, which will not cover the whole area of the button itself.

It has rounded corners and some padding and is supposed to be clickable by itself, wrap its content, and not span the whole width at the bottom of your screen.

Solution

As mentioned above, what you want is a different background. Not a standard button, but a background for a selectable item with this nice ripple effect.

For this use case there is the ?selectableItemBackground theme attribute which you can use for your backgrounds (especially in lists).

It will add a platform standard ripple (or some color state list on < 21) and will use your current theme colors.

For your usecase you might just use the following:

<Button
android:id="@+id/sign_in_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:background="?attr/selectableItemBackground" />
<!-- /\ that's all -->

There is also no need to add layout weights if your view is the only one and spans the whole screen

If you have some different idea on what your background should look like you have to create a custom drawable yourself, and manage color and state there.

How to remove extra padding or margin in material design button?

Step 1: Put the below code in styles.xml

    <style name="myColoredButton">
<item name="android:textColor">#FF3E96</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#FF0000</item>
</style>

Here you can change the textColor( I have used #FF3E96 above) and background color (I have used #FF0000) for your button. You can also override textColor values from your Button related layout xml by using android:colorButtonNormal.

Step 2:Create a new XML file under drawables folder and add the following code: I named my XML file as primary.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#8B8386" />
</shape>
</item>
</ripple>

Step 3: Use the style and drawable in your Button as follows.

    <Button
style="@style/myColoredButton"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Cancel"
android:gravity="center"
android:background="@drawable/primary_round"
android:colorButtonNormal="#3578A9" />

Hope it solves your problem.

Removing Image Button Padding (Android)

I ended up fixing this by switching my TableLayout to a LinearLayout. Here's my code unless anyone else comes across this problem:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >

<ImageButton
android:id="@+id/appHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription="Home"
android:scaleType="fitEnd"
android:src="@drawable/home_bar_grey" />

<ImageButton
android:id="@+id/menuHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription="Menu"
android:onClick="menuhome"
android:scaleType="fitCenter"
android:src="@drawable/menu_bar" />

<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription="Back"
android:scaleType="fitStart"
android:src="@drawable/exit_bar" />
</LinearLayout>

How do I reduce the inner padding around the text within an Android button object?

It took me forever to find this but the "padding" around the text in a button isn't really padding at all. The default Widget.Button style includes a minHeight property. Changing minHeight on the button will allow you to adjust padding as expected.

<Button
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/test"
android:textColor="@color/black"
android:minHeight="40dip"/>


<style name="Widget.Holo.Button" parent="Widget.Button">
<item name="android:background">@android:drawable/btn_default_holo_dark</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">@android:color/primary_text_holo_dark</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
</style>

Remove text padding in Button view

Button text doesn't need padding, it is by default in center of the button.
I think you are using the android:theme="@android:style/Theme.Black.NoTitleBar" in the manifest file, please remove it and use native app Theme and try it.

How to remove padding from Text button?

You cannot reduce padding with the padding modifier: it always adds an extra padding on top of the existing padding. See this reply for more details about the order of modifiers.

You can reduce TextButton padding with contentPadding argument, by specifying PaddingValues(0.dp), but this will not fully remove the padding.

If you need fully remove the padding, you can use the clickable modifier instead:

Text(
"getString(R.string.terms_and_conditions",
color = MaterialTheme.colors.primary,
fontFamily = FontFamily(Font(R.font.neris_semi_bold)),
fontSize = 10.sp,
modifier = Modifier
.clickable {
// onClick()
}
)

If you want to change the color of the ripple, as is done in TextButton, you can do it as follows:

.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(color = MaterialTheme.colors.primary),
) {

}

How to remove or reduce padding in Jetpack compose OutlinedButton

Button has default min size modifier. This is done according to Material guidelines, so that the button is easy to hit. If the control size is too small, the user may have problems hitting it, take this into account when changing this parameter.

You can override it by applying defaultMinSize modifier. The 0.dp will be ignored, but starting from 1.dp you will get the desired result:

OutlinedButton(
onClick = { /*TODO*/ },
contentPadding = PaddingValues(),
modifier = Modifier
.defaultMinSize(minWidth = 1.dp, minHeight = 1.dp)
) {
Text(
"Apple",
)
}

Alternatively, you can design your own button without these restrictions:

Surface(
onClick = {

},
shape = MaterialTheme.shapes.small,
color = bgColor,
contentColor = MaterialTheme.colors.primary,
border = ButtonDefaults.outlinedBorder,
role = Role.Button,
) {
Text(
"Apple",
)
}

Remove space between bounds and button border

The MaterialButton has a default style with these insets:

 <style name="Widget.MaterialComponents.Button"..>
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
<item name="android:insetTop">@dimen/mtrl_btn_inset</item>
<item name="android:insetBottom">@dimen/mtrl_btn_inset</item>
...
</style>

The value of @dimen/mtrl_btn_inset is 6dp.

Just use:

  <com.google.android.material.button.MaterialButton
android:insetTop="0dp"
android:insetBottom="0dp"
../>

enter image description hereenter image description here



Related Topics



Leave a reply



Submit