Viewbinding with multible views using the same id

467 views Asked by At

I am working on a project in which I want to start introducing view binding One of the layouts looks like this, the layout reuses ids for some reason I don't understand and I can't refactor it at the time being

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/tip_height"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:id="@+id/TipContainer">

    <LinearLayout
        style="@style/TipButtonNeatLeft"
        android:id="@+id/TipButtonNone">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/TipButtonTitle"
            style="@style/ResizableTipButton.Title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_title_height"
            android:text="@string/tip_signature_notip"/>

    </LinearLayout>

    <LinearLayout
        style="@style/TipButtonNeat"
        android:id="@+id/TipButton1">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@id/TipButtonTitle"
            style="@style/ResizableTipButton.Title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_signature_tip_option_title_height"
            android:text="@string/tip_signature_15percent"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/TipButtonSubTitle"
            style="@style/ResizableTipButton.Subtitle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_signature_tip_option_subtitle_height"
            tools:text="$4.99"/>
    </LinearLayout>

</LinearLayout>

I keep getting this error TipButtonTitle conflicts with another tag that has the same ID

I tried to use tools:viewBindingIgnore="true" on my root view but no luck with that any suggestion how to get rid of that error without having to change the ids

1

There are 1 answers

0
Yeldar Nurpeissov On

In views graph ids should be unique.

Try this:

    android:layout_width="match_parent"
    android:layout_height="@dimen/tip_height"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:id="@+id/TipContainer">

    <LinearLayout
        style="@style/TipButtonNeatLeft"
        android:id="@+id/TipButtonNone">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/TipButtonTitle"
            style="@style/ResizableTipButton.Title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_title_height"
            android:text="@string/tip_signature_notip"/>

    </LinearLayout>

    <LinearLayout
        style="@style/TipButtonNeat"
        android:id="@+id/TipButton1">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/TipButtonTitle2"
            style="@style/ResizableTipButton.Title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_signature_tip_option_title_height"
            android:text="@string/tip_signature_15percent"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/TipButtonSubTitle"
            style="@style/ResizableTipButton.Subtitle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tip_signature_tip_option_subtitle_height"
            tools:text="$4.99"/>
    </LinearLayout>

</LinearLayout>