Ali_Waris
Ali_Waris

Reputation: 2382

How to add padding between each item of a row in Android?

I am having a table with each row containing 3 buttons. Now, I want to add some padding between each buttons in a single row.. How can I do that..? When i add this statement in my code,

tableRow.setPadding(20,20,20,20);

I am able to observe padding but between each row. I want to have padding between each button...

Note: I want to do it from java programmatically, not from xml..

Upvotes: 0

Views: 1122

Answers (5)

Free Palestine
Free Palestine

Reputation: 1184

padding is the margin inside the button between the edges and the text.

This will increase button size not the distance between buttons

You need to use margin like this in your XML inside your button tag

   android:layout_margin="10dp"

this will set all margins to 10dp

if you need to set margins only for certain side use :

    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"

I hope this helps you out.

Regards

Upvotes: 1

Ivan
Ivan

Reputation: 3062

You need to use margin for your buttons instead of padding for the whole row. Because padding is actually a space between the button text and border, e.g. inner spacing, while margin is the space between button border and the it's container (row) e.g. outer space

Depending on what look you are trying to achieve, use something like this

<Button android:id="@+id/yourButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="Button1"
/>
<Button android:id="@+id/yourButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="Button2"
/>
<Button android:id="@+id/yourButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="Button3"
/>

Upvotes: 0

Umesh Singh Kushwaha
Umesh Singh Kushwaha

Reputation: 5741

The TableRow class, as with all View subclasses, indeed has a setPadding method.

However, since you mention that you found setMargin, I believe you are looking at the TableRow.LayoutParams instead of the TableRow itself.

Margins are set in a View's LayoutParams, whereas padding is set on the View.

Upvotes: 0

user3406222
user3406222

Reputation: 307

You can do it in this way:

<Button android:id="@+id/myBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:text="Click Me"
/>

Upvotes: 1

Rahul Tiwari
Rahul Tiwari

Reputation: 6968

you need to add padding to your buttons.

btn1.setPadding(left, top, right, bottom);
btn2.setPadding(left, top, right, bottom);
.
.
.

also make sure to take units in dp, hard-coded values will result in uneven padding on devices with various resolutions.

Upvotes: 0

Related Questions