jaiserpe
jaiserpe

Reputation: 335

Android: "duplicate attribute" XML error

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.

Upvotes: 15

Views: 35932

Answers (5)

vikas kumar
vikas kumar

Reputation: 11018

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>

Upvotes: 39

live-love
live-love

Reputation: 52366

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)

Upvotes: 49

prolink007
prolink007

Reputation: 34534

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.

Upvotes: 16

Aaron McIver
Aaron McIver

Reputation: 24713

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.

Upvotes: 1

Andrii Chernenko
Andrii Chernenko

Reputation: 10194

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

Upvotes: 0

Related Questions