Open In App

Concept of Padding in Android

Last Updated : 14 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In order to style and positioned User Interface Elements, we will use some standard attributes known as Margin and Padding. The Concept of Margin is already explained. In this article, all the confusion about the Padding is explained with Examples.

The padding specifies an extra space inside the view on which we applied Padding. Simply, Padding means Push Inside. When we say push inside we mean in a rectangle type view the content pushed itself according to the dimension specified in the padding attribute. Diagrammatically, the concept of Padding is shown as:

Syntax: android:padding=”size in dp”

The Above syntax will specify an extra space inside the view in all direction equally  i.e.,

  • Left
  • Top
  • Right
  • Bottom

It’s Working is Shown as below:

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="40dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
    </LinearLayout>
    
</androidx.constraintlayout.widget.ConstraintLayout>


Output:

But the above syntax has one problem i.e. When we use the above syntax then it will specify space on all sides. But sometimes while developing an Android application we want some extra space inside the view in some particular direction. So for that above syntax is not suitable for that below Attributes are used:

  • android:paddingRight=”size in dp”
  • android:paddingLeft=”size in dp”
  • android:paddingTop=”size in dp”
  • android:paddingBottom=”size in dp”

1. android:paddingRight =”size in dp”

This Attribute is used to specify extra space on the Right Side inside the view as shown in the below output.

XML




<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="80dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
</LinearLayout>


Output:

2. android:paddingLeft =”size in dp”

This Attribute is used to specify extra space on Left Side inside the view as shown in the below output.

XML




<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="90dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
</LinearLayout>


Output:

3. android:paddingBottom =”size in dp”

This Attribute is used to specify extra space on Bottom Side inside the view as shown in the below output.

XML




<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="90dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
</LinearLayout>


Output:

4. android:paddingTop =”size in dp” 

This Attribute is used to specify extra space on the Top Side inside the view as shown in the below output.

XML




<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="90dp"
            android:background="#009933"
            android:text="GeeksforGeeks"
            android:textColor="@color/white"
            android:textSize="30sp" />
  
</LinearLayout>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads