Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the padding/margin to the top activity/fragment layout container in theme?

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.

like image 894
Mahesh Ramchandra Bhatkande Avatar asked Nov 04 '15 06:11

Mahesh Ramchandra Bhatkande


People also ask

What is padding in layout?

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).

How does padding of a view differ from the view's margin?

Cool, so margin means the space outside the view, however padding means the space inside the view.

Which layout is used in fragment?

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.

What is padding in Android layout?

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.


2 Answers

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"
like image 125
karan Avatar answered Oct 21 '22 07:10

karan


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

like image 25
Guillaume Imbert Avatar answered Oct 21 '22 07:10

Guillaume Imbert