
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
Concept of Padding in Android
What is Padding in Android?
Padding is a space which is present between the content which we have to display and the border of that content. Padding is used to create the extra space within the content.
We can add padding for a widget from any of the specific sides. We can add padding either from left,right,top or bottom depending upon the requirement.
In this article we will take a look on How to use padding attributes for different widgets within our android application.
Syntax
android:padding="Size in dp"
Explanation − Above syntax for padding is used to specify padding from all sides of a view.
android:paddingStart="Size in dp" android:paddingLeft="Size in dp"
Explanation − Above syntax for padding is used to specify padding from the left side. We can use either paddingStart or paddingLeft to add padding for a view from the left side.
android:paddingEnd="Size in dp" android:paddingRight="Size in dp"
Explanation − Above syntax for padding is used to specify padding from the right side. We can use either paddingEnd or paddingRight to add padding for a view from the right side.
android:paddingTop="Size in dp"
Explanation − Above syntax for padding is used to specify padding from top of a view.
android:paddingBottom="Size in dp"
Explanation − Above syntax for padding is used to specify padding from bottom of a view.
Example
We will be creating a simple application in which we will be simply displaying a button and adding padding from all sides to it one by one and test it.
Step 1 : Creating a new Project in Android Studio
Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.
After clicking on New Project you will get to see the below screen.
Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.
Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.
Note − Make sure to select the Language as Kotlin.
After specifying all the details click on Finish to create a new Android studio project.
Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.kt file.
Step 2 : Working with activity_main.xml
Example 1 : Padding from all sides
Concept of Padding in Android
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- creating a simple text view --> <TextView android:id="@+id/idBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:background="#FF000000" android:padding="30dp" android:text="Hello World" android:textColor="#FFFFFFFF" android:textSize="25sp" android:textStyle="bold" /> </RelativeLayout>
Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.
Inside this relative layout we are creating a Textview. For this text view we are specifying background color and then we are adding padding for this text from all sides.
Example 2 : Padding from left side
Working with activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- creating a simple text view --> <TextView android:id="@+id/idBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:background="#FF000000" android:paddingLeft="30dp" android:text="Hello World" android:textColor="#FFFFFFFF" android:textSize="25sp" android:textStyle="bold" /> </RelativeLayout>
Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.
Inside this relative layout we are creating a Textview. For this text view we are specifying background color and then we are adding padding for this text from the left side.
Example 3 : Padding from the Right side
Working with activity_main.xml file.
Syntax
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- creating a simple text view --> <TextView android:id="@+id/idBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:background="#FF000000" android:paddingRight="30dp" android:text="Hello World" android:textColor="#FFFFFFFF" android:textSize="25sp" android:textStyle="bold" /> </RelativeLayout>
Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.
Inside this relative layout we are creating a Textview. For this text view we are specifying background color and then we are adding padding for this text from the right side.
Example 4 : Padding from the top side
Working with activity_main.xml file.
Syntax
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- creating a simple text vixew --> <TextView android:id="@+id/idBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:background="#FF000000" android:paddingTop="30dp" android:text="Hello World" android:textColor="#FFFFFFFF" android:textSize="25sp" android:textStyle="bold" /> </RelativeLayout>
Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.
Inside this relative layout we are creating a Textview. For this text view we are specifying background color and then we are adding padding for this text from the top side.
Example 5 : Padding from the bottom side
Working with activity_main.xml file.
Syntax
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- creating a simple text vixew --> <TextView android:id="@+id/idBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:background="#FF000000" android:paddingBottom="30dp" android:text="Hello World" android:textColor="#FFFFFFFF" android:textSize="25sp" android:textStyle="bold" /> </RelativeLayout>
Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.
Inside this relative layout we are creating a Textview. For this text view we are specifying background color and then we are adding padding for this text from the bottom side.
Conclusion
In the above tutorial we learn What is padding in android and How we can use it inside our application to add spacing between the content of the widget and the border of the widget.
- Related Articles
- Difference between a View\'s margin and padding in Android
- How to use concept () in Android text view?
- CSS padding property
- Concept of Conflicts
- Concept of Dharma
- Concept of Cost
- The padding shorthand Property in CSS
- Proof of Concept (PoC) in Agile
- Concept of Self in Different Tradition
- Concept of Alternative Hypothesis in Psychology
- Concept of Address Split in OS
- CSS padding-right property
- CSS padding-bottom property
- CSS padding-top property
- CSS padding-left property
