I want to separate the usage of my application on xlarge devices and usage on other devices to restrict layout_width parameter by 720dp for xlarge. For this purpose I create values/attrs.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="layoutWidth" format="reference|dimension" />
</resources>
with custom parameter layoutWidth to set it in
android:layout_width="?layoutWidth"
Furtner, I need to specify two themes.xml files for xlarge and ordinary devices in values-xlarge and values directories:
values-xlarge/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="layoutWidth">720dp</item>
</style>
</resources>
values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="layoutWidth">What should I write here!?</item>
</style>
</resources>
So, how can I make a reference on Android "fill_parent" parameter at this place? It seems like @android:layout_width/fill_parent, but I have compiling Error:
No resource found that matches the given name (at 'layoutWidth' with value '@android:layout_width/fill_parent').
attr/ references to attributes. Attributes are values specified in an app's theme. The attributes in your example are all values specified in the themes provided by the support library. Android also has its very own attributes which can be used with ? android:attr/ .
AttributeSet (Android Docs)A collection of attributes, as found associated with a tag in an XML document. Basically if you are trying to create a custom view, and you want to pass in values like dimensions, colors etc, you can do so with AttributeSet .
When using the Layout Editor in design view, the Properties window also allows you to edit some design-time view attributes.
I have found a solution by means of changing values/attrs.xml to:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="layoutWidth" format="dimension">
<enum name="fill_parent" value="-1" />
<enum name="match_parent" value="-1" />
<enum name="wrap_content" value="-2" />
</attr>
</resources>
Now, I can write in values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="layoutWidth">match_parent</item>
</style>
<resources>
But the question still remain: Is it possible to refer to Android layout_width parameter from this place?
You should create attrs.xml file inside values folder and then add the below code in it:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="custom_match_parent" type="dimen">-1</item>
<item name="custom_wrap_content" type="dimen">-2</item>
</resources>
To access the above created attrs.xml add the below line inside your dimens.xml file as follows:
<dimen name="max_layout_width">@dimen/custom_match_parent</dimen>
Now to access that from a layout file you must do something like
<FrameLayout
android:layout_width="@dimen/max_layout_width"
android:layout_height="match_parent">
<!--Do what you like to do here-->
</FrameLayout>
Hope this helps! :-)
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