Giannis Giannis - 1 month ago 22
Android Question

Getting errors at a simple Android app concerning the layout xml file

So I tried the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slidenerd.sample.customdesign.MainActivity"
android:background="#252525">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:layout_above="@id/textView"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:text="0:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textColor="#616161"
android:textSize="100sp"
android:layout_centerInParent="true" />

<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:id="@+id/toggleButton"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
</RelativeLayout>


The errors I am getting are these

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
E:\AndroidProjects\Tutorials\CustomDesign\app\src\main\res\layout\activity_main.xml
Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/textView').
E:\AndroidProjects\Tutorials\CustomDesign\app\build\intermediates\res\merged\debug\layout\activity_main.xml
Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/textView').
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
Information:BUILD FAILED
Information:Total time: 21.791 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console


In contrast the following works correctly

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slidenerd.sample.customdesign.MainActivity"
android:background="#252525">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:layout_marginTop="54dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:text="0:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:id="@+id/textView"
android:textColor="#616161"
android:textSize="100sp"
android:layout_centerInParent="true" />

<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:id="@+id/toggleButton"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
</RelativeLayout>


I don't understand why the first doesn't work. Is it wrong to arrange the various items based on an item that is placed at the center of the layout.

Answer

No resource found that matches the given name (at 'layout_above' with value '@id/textView').

It's trying to tell you that you're using an id that hasn't been initialized yet. To avoid this, you should be able to use the first layout as long you specify "android:layout_above="@+id/textView" (The "+" needs to be there in @+id)

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/clock"
        android:layout_above="@id/textView"   <-- this guy
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
Comments