3

I have many of

android:layout_width="fill_parent"

&

android:layout_height="wrap_content"

properties in my app project. So i would like to add them in styles like the below.

<style name="layout_wf_hw">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>
<style name="layout_ww_hw">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

styles.xml

<style name="inputbox" parent="layout_wf_hw">
    <item name="android:background">@drawable/editviewstyle</item>
</style>

Now my code view is

<EditText style="@style/inputbox"/>

My question is can we apply like this? Is this right way what i followed?

Thanks

Pavan Kumar
  • 1,616
  • 5
  • 26
  • 38
  • Theoretically that might work. But you loose original `inputbox` styling. – David Jashi Jun 07 '13 at 07:56
  • How? can you please let me know? – Pavan Kumar Jun 07 '13 at 08:00
  • Well, original, built in `inputbox` have some default style properties defined in theme, and you override them by `parent="layout_wf_hw"`, aren't you? But if it suites your needs - why not? – David Jashi Jun 07 '13 at 08:04
  • Define a string attribute and use in theme/style as shown [here](http://stackoverflow.com/a/12339924/1531054). By the way, Android Lint will complain if you don't define dimension attributes for each View in layout. – S.D. Jun 07 '13 at 08:05
  • whatever properties we are adding(android:layout_width="fill_parent" & android:layout_height="wrap_content") in layout instead of giving here we are just adding through styles. I feel both are the same. – Pavan Kumar Jun 07 '13 at 08:22

2 Answers2

1

I don't see anything which is not allowed in your code.

Actually I think it's respecting DRY principle, and that's a good thing.

The only thing which might be annoying is that maybe it's a little bit more difficult to understand and read for other developers on the first time.

So my answer is: why not.

Aerilys
  • 1,628
  • 1
  • 16
  • 22
0

ok your code is correct but here is an idea...

you put that the background will be static for all xml's there in your app...but maybe you wanna change your background....or maybe you want to change the width and height of one of an xml

im not saying its wrong but to be more accurate let every xml has the width and height my friend

and one thing: you added in layout_wf_hw in width:fill parent and in height:wrap_content but maybe you want to use in height: match parent or any value other than wrap !!!

be simple in your code and dont make it complicated just for you :)

R.F
  • 312
  • 1
  • 16
  • Same like you i thought about this. so default is "inputbox". in any case need to chage the properties i will write like this ... – Pavan Kumar Jun 07 '13 at 10:22