-2

I'm new to Android Studio. I'm having errors in @dimen and I don't know how to fix it.

Here's the code:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.videotest.MainActivity">
Renzo
  • 87
  • 1
  • 8
  • 1
    maybe is not exists? add the stack trace – Yoni Aug 30 '17 at 17:18
  • 3
    You didnt show code, you didn't show errors - How are we supposed to help you without the proper context? Include all relevant details for quicker and/or higher quality assistance. – Vince Aug 30 '17 at 17:24

2 Answers2

2

First of all @dimen refers to dimension and it's a file where you define dimensions to use them later from in any layout file.

In your Project Structure, go to the directory app/src/main/res/values/ Now check here if dimens.xml file is present or not.

If not, then create a new resource file with the name dimens.xml

So depending on the padding you want define the dimens like this :

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
2

You will have to define the dimensions which you are setting via @dimen in the dimens.xml.

According to the Android Docs, "A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one element."

From the Android docs, an example of dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2013 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
    <dimen name="action_button_min_width">56dp</dimen>
    <dimen name="indeterminate_progress_size">32dp</dimen>
</resources>

Define your other dimensions here and save this XML under the values directory. Also take a look in this answer which explains when to use the dimens.xml file in Android?

Abhi
  • 3,431
  • 1
  • 17
  • 35