15

I have a Layout with a set of widgets on the left side, and others on the right side.

Now I want to put a button in the centre, below to two textviews (one being on the left and the other one being on the right).

I get an error ("duplicate attribute") with the following code:

android:layout_centerInParent="true"
android:layout_below="@id/text_left"
android:layout_below="@id/text_right"

How can I solve this?

Thanks.

4
  • u can not use android:layout_below="@id/text_right" two times in same widget.remove one Commented Dec 20, 2012 at 17:26
  • it is not the same widget. One is "@id/text_right" and the other is "@id/text_left" Commented Dec 20, 2012 at 17:31
  • can u put your xml code so i can correct it Commented Dec 20, 2012 at 17:38
  • @jaiserpe Added additional commentary in my answer to help point out that it is not the @id/text... causing the issues, as those are unique, it's the fact you are calling layout_below. Commented Dec 20, 2012 at 17:45

5 Answers 5

49

This error message also happens if you have the same xmlns attribute in different layouts.

In this case, xmlns:tools is repeated in layout and ConstraintLayout. Removing it from one of the layouts should fix the problem.

    <layout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"> 

        <data>
            <variable
                name="item"
                type="com.myapp.SearchSettings" />
        </data>

        <android.support.constraint.ConstraintLayout

            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" <-- remove this line

(it is already in layout tag)
Sign up to request clarification or add additional context in comments.

Comments

39

For those, accepted answer didn't work and by any change you are using the android Data Binding then this kind of error may come if some of the attributes are present twice in parent tag as well as child tag. In below example the android:layout_width="match_parent" android:layout_height="wrap_content" are used twice in parent as well as in child.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <data>

        <variable
            name="pdpDescriptionViewModel"
            type="com.tottus.ui.productdescription.model.PdpDescriptionViewModel" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </LinearLayout>


</layout>

to solve this remove duplicate attribute from the parent or child and it should work.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="pdpDescriptionViewModel"
            type="com.tottus.ui.productdescription.model.PdpDescriptionViewModel" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </LinearLayout>


</layout>

1 Comment

Thanks, I had exactly this problem with a android:background="@color/black" in <layout> and <ConstraintLayout>
16

You are setting the layout_below twice.

If you want the layout in question to be below both of those, try combining both of the text_left and text_right into one layout and then use layout_below and assign it the name you gave to the layout that contains the combination of text_left and text_right.

Comments

1

You are setting layout_below twice.

android:layout_below="@id/text_left"
android:layout_below="@id/text_right"

You can only set this once per widget which in this case is your Button. You are essentially trying to tell the Button to place itself below two different items by way of calling android:layout_below multiple times.

If the end result is not what you expect using only one of the TextViews you may have to adjust your reference point to something which spans the entire width, or perhaps wrap your TextViews in a LinearLayout and use that as your reference point. It may be easier to also switch layout types at the root level, moving to a LinearLayout and nesting them as needed.

Comments

0

You need to choose one of your android:layout_below. You cannot have both.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.