Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer android attribute layout_width from custom attribute?

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').
like image 656
isabsent Avatar asked Jan 16 '12 14:01

isabsent


People also ask

What does ATTR mean in Android?

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

What is AttributeSet?

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 .

What is the window called where we can change the attributes of each tool that is in the layout?

When using the Layout Editor in design view, the Properties window also allows you to edit some design-time view attributes.


2 Answers

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?

like image 170
isabsent Avatar answered Oct 17 '22 07:10

isabsent


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! :-)

like image 28
Nithin Prasad Avatar answered Oct 17 '22 08:10

Nithin Prasad