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.
Asked
Active
Viewed 1,998 times
3
-
Did you ever find anything? – Ginger McMurray Jan 27 '14 at 20:30
-
1Check out this [solution](http://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android). The solution presented there is much closer to what you are seeking. – Nimrod Dayan Jan 24 '15 at 13:01
1 Answers
-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
-
3why 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