56

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>
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Rendy
  • 5,572
  • 15
  • 52
  • 95
  • 1
    Have you tried [this](http://mytechsolutions.blogspot.com.es/2011/04/unable-to-resolve-dimension-value.html)? An option could be using styles. – AlexBcn May 10 '13 at 09:22
  • @AlexBcn yeah, but still the same. I think the SDK doesn't like with -1dp value, please see my edited post. – Rendy May 10 '13 at 09:24
  • How does it "complain"? Are you using the latest Eclipse plugin? I seem to be able to use -1dp without issue in the layout preview. – NPike May 10 '13 at 16:30
  • Really? My SDK is 21.1 and platform-tools 16.0.1.. How about yours? – Rendy May 10 '13 at 17:06
  • 2
    I'm using your approach with -1dp and -2dp and it works on actual devices (Nexus10), thanks. – ernazm Aug 12 '13 at 15:04
  • Use a style instead of dimen – enl8enmentnow Nov 30 '15 at 15:01

6 Answers6

56

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.

brescia123
  • 517
  • 6
  • 23
Swetank
  • 1,577
  • 11
  • 14
  • 2
    Android Studio 0.5.3, SDK Tools 22.6.1 don't like the use of an integer as a dimen value :( – straya Apr 04 '14 at 04:28
  • Try -1dp and -2dp. They are AS friendly :) – Ivan Morgillo Oct 22 '14 at 07:25
  • 29
    Use `-1px` and `-2px` instead of `dp`. The `px` values will not scale based on the device's screen density. – Jarett Millard Feb 24 '15 at 17:33
  • @Ambran On Android O you can use 0dp in most cases to make it "match constraint" (if you're using Constraint Layout). But I still have no idea how to solve the "wrap_content" problem. – Leonardo Sibela Nov 08 '19 at 16:56
  • In my case the -1px value resets the View's margins, So if your view needs some margins use [Fonix's solution](https://stackoverflow.com/a/39523353/9317258). It works fine. – Vít Kapitola Nov 25 '20 at 14:25
43

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>
Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • 2
    This gives me a Resources$NotFoundException.. :( – mco Dec 08 '16 at 03:52
  • 1
    `-1px` and `-2px` respectively. first, not `dp` but `px` because of scaling, second, without dimension classification emulator api 25 failing in runtime for me with error `layout_width not specified` or something like that – vigilancer Jun 13 '17 at 10:41
  • 1
    Seems to work so far :) I've used `custom_match_parent` and `custom_wrap_content` as names to not confuse people. – Albert Vila Calvo Mar 05 '19 at 09:27
27

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

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • 3
    This should be the accepted answer because it doesn't use magic numbers such as -1dp and -2dp and specifies the dimensions explicitly. – Georgi Feb 23 '17 at 11:30
  • I like the way this one reads, but in certain situations, your app can crash if there is no `layout_height` or `layout_width` explicitly defined within the layout file – Sam May 02 '20 at 06:32
10

For HTC devices use this to achieve match_parent:

<dimen name="my_match_parent">-1.0px</dimen>
ramon
  • 830
  • 1
  • 12
  • 12
1

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>
Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
0

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

Bolton
  • 2,226
  • 3
  • 25
  • 29