padding not reflecting on the button

Viewed 84

When I see on the XML layout file in the android studio I can see the padding is there but on the device, I can not see it. I am not adding padding programmatically on that button. I only set the background of the button programmatically.

In Android Studio

enter image description here

In Device

enter image description here

Here is my button code

 <Button
        android:id="@+id/LoadBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:layout_marginTop="16dp"
        android:background="@color/colorAccent"
        android:foreground="?android:attr/selectableItemBackground"
        android:text=" LOAD MAP "
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="30sp"
        android:textStyle="bold"
        android:paddingStart="7dp"
        android:paddingEnd="7dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        app:layout_constraintStart_toStartOf="@+id/fileTypeRadioGroup"
        app:layout_constraintTop_toBottomOf="@+id/fileTypeRadioGroup" />

Here is the java code that I am doing with that button.

loadBtn = findViewById(R.id.LoadBtn);
        loadBtn.setEnabled(false);
        loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.disabled_button_background));
loadBtn.setEnabled(true);
            loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.color.colorAccent));
2 Answers

Try setting the text of the button to dp, instead of sp. Sp will scale the text based on the device's preferences, while dp will not. It looks based on the example that they are just differently sized texts.

It could be that inside your android studio you are watching a display of one phone but when you run it you actually run it on another phone with a totally different screen resolution.

So you may see it as you would like to in your preview but this screen preview and your phone screen are different, and because different phones got different screen sizes and pixel densities you are seeing this differently.

When using a fixed size (7dp) value for padding for example for larger screens you will need to use more than 7dp if you want it to look like the preview or less if you are using a smaller screen than what you have in your android studio preview.


A good solution could be to use sdp library:

From the library github page:

An android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.

How to use:

You will first need to implement this:

dependencies {
      implementation 'com.intuit.sdp:sdp-android:1.0.6'
}

And now on your layout file simply add the wanted padding:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:text="Text"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="@dimen/_18sdp"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</Button>

And it will look like this:

enter image description here


Something extra

If you want to make your texts scale in size as well according to the screen size (as I just showed for dp) you can use

AutosizingTextViews or you can even use ssp library

Related