Presentation1 Padding
Presentation1 Padding
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.
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