Nativescript: [Android]: How can I remove the top/bottom padding of a button?

Created on 12 Jun 2018  路  9Comments  路  Source: NativeScript/NativeScript

_From @relez on June 6, 2018 14:13_

Hi guys, I would like to remove the top/bottom padding of a Button in Android, but I have tried all CSS styles and no luck :frowning:
I have no idea about how to configure buttons in the styles.xml file.

Any idea please? Thanks!

dashboard-button

_Copied from original issue: NativeScript/android-runtime#1080_

android

Most helpful comment

Don't think this issue should be closed if it's still present in 6.1+... a hack-y workaround isn't a proper fix

All 9 comments

@relez there are some native specifics that lead to this default behavior. On Android, most of the default application themes are applying several predefined conditions that are causing that all buttons to be rendered with a "padding". The workaround is to remove the minHeight and minWidth of your button.

For example (using the loaded event of the Button to overwrite its default minHeight and minWidth):

onBtnLoaded(args) {
    let btn = <Button>args.object;
    let nativeButton = btn.nativeView;

    if (isAndroid) {
        nativeButton.setMinHeight(0);
        nativeButton.setMinWidth(0);
    }
}

Now padding: 0 will actually remove all the paddings.

Full demo application demonstrating the above can be found in this Playground.

@NickIliev Imagine having to do this is for all buttons in an app. Is there another solution?

@topclaudy well the thing is that we wanted to preserve the native look'n'feel for the Button element. So I guess if you want a fully custom button that has no default padding you could use Label with tap event.

@NickIliev The problem is not preserving the native look'n'feel (that's fine). The problem is that consumers are unable to remove the padding via CSS. I ended up using labels but I'm not happy with this workaround.

@relez there are some native specifics that lead to this default behavior. On Android, most of the default application themes are applying several predefined conditions that are causing that all buttons to be rendered with a "padding". The workaround is to remove the minHeight and minWidth of your button.

For example (using the loaded event of the Button to overwrite its default minHeight and minWidth):

onBtnLoaded(args) {
    let btn = <Button>args.object;
    let nativeButton = btn.nativeView;

    if (isAndroid) {
        nativeButton.setMinHeight(0);
        nativeButton.setMinWidth(0);
    }
}

Now padding: 0 will actually remove all the paddings.

Full demo application demonstrating the above can be found in this Playground.

This is a good way to handle this particular thing. I just feel there should've been internal implementation to make sure that there aren't two different snippets for the two platforms. Seems a little tedious to write it specifically for Android.

Hi @sanved77, thanks for the answer. I ended up using labels which is not a nice solution but it works. I was researching and I believe there is a way to remove setting up a new style in the manifest file, overriding the default native theme or something like that, that would be a nice solution.

According to your solution, Do I have to add the loaded event to the button and then paste the event every time I use a button? If so, that would be very annoying, I will try to look for a global solution.
Thanks!

android:minWidth = "0px" in xml?

Hi @sanved77, thanks for the answer. I ended up using labels which is not a nice solution but it works. I was researching and I believe there is a way to remove setting up a new style in the manifest file, overriding the default native theme or something like that, that would be a nice solution.

According to your solution, Do I have to add the loaded event to the button and then paste the event every time I use a button? If so, that would be very annoying, I will try to look for a global solution.
Thanks!

Unfortunately yes, you have to add that to the label every possible time. I ended up finding a more intrinsic way to get it centered rather than the pixels. I have made it this way --

<Label @loaded="$onLabelLoaded" </Label>

And the prototype -->

Vue.prototype.$onLabelLoaded = function (args) {
if (platform.isAndroid) {
args.object.android.setGravity(17)
}
}

Don't think this issue should be closed if it's still present in 6.1+... a hack-y workaround isn't a proper fix

Was this page helpful?
0 / 5 - 0 ratings

Related issues

valentinstoychev picture valentinstoychev  路  3Comments

guillaume-roy picture guillaume-roy  路  3Comments

kn9ts picture kn9ts  路  3Comments

NordlingDev picture NordlingDev  路  3Comments

Leo-lay picture Leo-lay  路  3Comments