3

I am trying to match the style of a website with my app and on the website there is always a 10 pixel border around every page that just shows the background. I am trying to set up my activities to include that with themes, so I don't need to pad every layout I write. Is there a way for me to theme this? I've been looking through all of the available theme items and haven't been able to find it, but this is my first time using themes.

pohart
  • 170
  • 2
  • 13

1 Answers1

-3

I would approach this task by setting up a margin or padding in your top layer container in the hierarchy of views in activity. First, define an attribute.

res/attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="desiredActivityBorder" format="dimension" />
</resources>

Then, in your layout for activty use this atribute:

layout/your_activiy.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:width="match_parent"
    android:height="match_parent"
    android:padding="?desiredActivityBorder">

    ... your views go in here ...

</LinearLayout>

Of course this assumes that your LinearLayout background is transparent so one can see through to background beneath. All you have to do know is to set a value for this attribute in your theme.

<style name="YourTheme" parent="Holo.Theme ">
    <item name=" desiredActivityBorder">10dp</item>
</style>
Anderson
  • 1,011
  • 2
  • 11
  • 24
  • 3
    why extra step with style ? You can set that directly inside *res/values/dimens.xml*, e.g. `10dp` and then in activity layout: `android:padding="@dimen/activity_padding"` – Alexander Malakhov Oct 15 '14 at 09:10
  • @ Alexander Malakhov Yes you could. I haven't done it myself yet. But reason for using style is that this style is a theme (theme is nothing more than a style) so this padding becomes a property of theme. It seems more formally correct. The idea is if you want to modify theme you modify only theme style. You can combine both approaches: declare a constant dimension in dimen.xml, like big_padding, small_padding and use that in theme style. But there is nothing wrong with your approach per se. – Anderson Oct 15 '14 at 15:50
  • @AlexanderMalakhov is right, the attribute declaration is completely redundant here. That's not what attributes are for. +this isn't actually a solution to the problem presented by the OP. The question was how to apply global theme attribute so he won't need to do exactly what you suggested - applying style in a form of attribute to every view that needs padding. – Nimrod Dayan Jan 24 '15 at 12:52