Home » Android » layout – How to refer android attribute layout_width from custom attribute?

layout – How to refer android attribute layout_width from custom attribute?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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').
How to&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?

Answer:

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! 🙂

Answer:

Well, this is a late response…but, for posterity and sanity, the answer can be found below and more info can be found at http://developer.android.com/guide/topics/ui/themes.html

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourStyle" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
</resources>

This should work for any parent style that uses android:layout_width, such as parent=”@android:style/Widget.TextView”

Answer:

Not sure if you can add this attribute somewhere in your resources or style tags, but it may help (if its legal)

xmlns:android=”http://schemas.android.com/apk/res/android”

This specifies the namespace where layout_width and layout_height are defined (as well as the constants fill_parent and wrap_content).