In my app I wanted to apply padding to all my screen top level container from the theme.
example :
here is the layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
here is the app theme.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFrame">@drawable/window_frame</item>
</style>
I can apply padding using android:padding
to the parent container RelativeLayout. But I don't want like this as I need to specify the same in each layout file, I wanted to define that in theme - AppTheme.
Any solution will be appreciated.
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Cool, so margin means the space outside the view, however padding means the space inside the view.
A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library, fragments are supported back to all relevant Android versions.
The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.
why you need to code extra using style?
You can set padding directly inside res/values/dimens.xml,it would be much easier. and whenever you need to increase or decrease the values you just need to change at single place for every activity
e.g. in dimens.xml
<dimen name="my_activity_padding">5dp</dimen>
and then in activity layout:
android:padding="@dimen/activity_padding"
The theme is not the solution since it will add padding to all view hierarchy under your layout. (your RelativeLayout as well as the EditText.
Use @Karan solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With