Value equals to match_parent or fill_parent in dimens.xml?

AndroidXmlAndroid LayoutAndroid ResourcesDimension

Android Problem Overview


Based on here on XML Attributes section I specify following in my dimens.xml:

<dimen name="match_parent">-1dp</dimen>
<dimen name="main_left_menu_user_account_width">@dimen/match_parent</dimen>
<dimen name="main_left_menu_user_account_height">@dimen/match_parent</dimen>

Then I use the both dimensions in my layout:

<ImageView 
    android:id="@+id/userAccountImage"
    android:background="@drawable/user_account"
    android:layout_width="@dimen/main_left_menu_user_account_width"
    android:layout_height="@dimen/main_left_menu_user_account_height" />

Then, when I preview to Graphical Layout, it complains:

> You must supply a layout_width attribute. > > You must supply a layout_height attribute.

Actually can I define a value equals to match_parent in dimens.xml?

Update:

I also tried this but the preview still complains:

<dimen name="main_left_menu_user_account_width">-1dp</dimen>
<dimen name="main_left_menu_user_account_height">-1dp</dimen>

I successfully use wrap_content (the Graphical Layout doesn't complain at all):

<dimen name="wrap_content">-2dp</dimen>

<dimen name="main_right_menu_width">@dimen/wrap_content</dimen>
<dimen name="main_right_menu_height">@dimen/wrap_content</dimen>

Android Solutions


Solution 1 - Android

Use this, it works for me

<dimen name="custom_wrap_content">-2px</dimen>  
<dimen name="horizontal_border_height">@dimen /custom_wrap_content</dimen>

<dimen name="custom_match_parent">-1px</dimen>  
<dimen name="vertical_border_height">@dimen /custom_match_parent</dimen>

And the Reason why match_parent doesn't run. You cannot supply a build in keyword like match_parent

Edit: Use px instead of dp as suggested by Jarett Millard in the comments.

Solution 2 - Android

First create attribs.xml:

<resources>
    <item name="match_parent" type="dimen">-1</item>
    <item name="wrap_content" type="dimen">-2</item>
</resources>

Second use your dimens:

   <dimen name="account_width">@dimen/match_parent</dimen>
   <dimen name="account_height">@dimen/wrap_content</dimen>

Solution 3 - Android

Depending on why you want to define match_parent in a @dimen, this use case could help you:

Instead of defining the width and height in dimen.xml, you can define it as a style in the styles.xml

I use

//res/values/styles.xml
<style name="IntroLayout">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

and

//res/values-sw600dp/styles.xml
<style name="IntroLayout">
    <item name="android:layout_width">520dp</item>
    <item name="android:layout_height">wrap_content</item>
</style>

and use it like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_gravity="center"
                style="@style/IntroLayout">

which allows me to dynamically set the width and height attributes for different sized devices without having to write any code and you can use match_parent/wrap_content fine. you can use any @dimen that you have defined previously in the style as well if you want.

I use this because the layout for phone and tablet is the same, except i want to fix the width on tablet but fill the parent on phone, so it saves having to have 2 different layouts with basically the same xml

Solution 4 - Android

For HTC devices use this to achieve match_parent:

<dimen name="my_match_parent">-1.0px</dimen>

Solution 5 - Android

You can also achieve this using integers.xml file

integers.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="match_parent">-1</integer>
    <integer name="wrap_content">-2</integer>
</resources>

Use in dimens.xml:

<dimen name="main_right_menu_width">@integer/wrap_content</dimen>

You might also get lint warning, to suppress it use:

<dimen name="main_right_menu_width" tools:ignore="ReferenceType">@integer/wrap_content</dimen>

Solution 6 - Android

I don't think so. @dimen/match_parent is a specific length with unit while match_parent is a special flag.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRendyView Question on Stackoverflow
Solution 1 - AndroidSwetankView Answer on Stackoverflow
Solution 2 - AndroidAhmad AghazadehView Answer on Stackoverflow
Solution 3 - AndroidFonixView Answer on Stackoverflow
Solution 4 - AndroidramonView Answer on Stackoverflow
Solution 5 - AndroidFiroz MemonView Answer on Stackoverflow
Solution 6 - AndroidBoltonView Answer on Stackoverflow