Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabHost layout and DataBinding

I read about android data binding and want to use it in my application, but I failed on xml layout stage.

I have activity_main.xml like 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 apply data binding to the last EditText, but if I insert

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

this causes

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

The question is, how should I combind data binding and TabHost to bind EditText in included layout?

Here is repo with code from question

like image 477
user1244932 Avatar asked Jul 21 '17 14:07

user1244932


People also ask

What is view binding and Data Binding?

The one and only function of View Binding is to bind the views in the code, while Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.

What is 2 way Data Binding in Android?

Stay organized with collections Save and categorize content based on your preferences. The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time.


2 Answers

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

Now, which XML attribute is duplicated? The namespace (xmlns:android)

Remove the one that is not at the very top element of the XML in the layout tag

like image 116
OneCricketeer Avatar answered Sep 19 '22 23:09

OneCricketeer


Issue is with xmlns:android

Just remove this xmlns:android="http://schemas.android.com/apk/res/android" from and its done.

Regarding DataBinding, i don't think so you have even implemented it except that tag

take <data> in your activity_main.xml

<data>

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

</data>

Pass it with included layout

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

Now catch that data inside your 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>

You are almost done, now you just need to implement binding in your activity

ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.setName("Email Address");
like image 34
Ravi Avatar answered Sep 20 '22 23:09

Ravi