TabHost plugin and DataBinding

I read about data binding to Android and want to use it in my application, but I was not able to complete the xml build step.

I have activity_main.xmllike this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

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

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/tab1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/tab1"/>

        </LinearLayout>

    </FrameLayout>
</LinearLayout>
</TabHost>
</layout>

and tab1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<EditText
...

I want to apply data binding to the latter EditText, but if I insert

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

it causes

activity_main.xml:9: AAPT: Error parsing XML: duplicate attribute

The question is, how do I combine data binding and TabHostbind EditTextto the included layout?

Here is the repo with the code from the question

+6
source share
3 answers

Here is your hint XML: duplicate attribute. It even tells you the line number in the error message 9, which is roughly located in the TabHost element.

, XML ? (xmlns:android)

, XML

+2

xmlns:android

xmlns:android="http://schemas.android.com/apk/res/android" .

DataBinding, , , ,

<data> activity_main.xml

<data>

    <variable
        name="name"
        type="String"/>

</data>

<include layout="@layout/tab1"
         app:name="@{name}"/>

tab1.xml

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

    <data>
        <variable
            name="name"
            type="String"/>
    </data>

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

        <EditText
            android:id="@+id/edit1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            android:ems="1"
            android:inputType="text"
            android:text="@{name}" />
    </LinearLayout>
</layout>

, .

ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.setName("Email Address");
+1

The two errors that I see here used the namespace xmlnstwice, and tab1twice. Remove one namespace and change the identifier.

 <LinearLayout
                android:id="@+id/tab1" /* you used tab1 here as id*/
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include layout="@layout/tab1"/> /* you used tab1 here as id */
0
source

All Articles