[Android] Variable of Data Binding is written in camelCase

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

Than,

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

The story that I don't think I'm in trouble. Because I was addicted to it.

Concrete example

"When I tried to use the variable of snake_case in the<include>file, I couldn't access it."

Try to write xml

child.xml


<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="value_text"
            type="String" />
    </data>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{value_text}"/>
</layout>

parent.xml


<Parent element omitted>
    <include
        layout="@layout/child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:value_text="@{someVariable}"/>
</Parent element omitted>

Generated code

ChildBindingImpl.java


    public void setValueText(@Nullable java.lang.String ValueText) {
        this.mValueText = ValueText;
        synchronized(this) {
            mDirtyFlags |= 0x2L;
        }
        notifyPropertyChanged(BR.value_text);
        super.requestRebind();
    }

ParentBindingImpl.java


    @Nullable
    private final app.package.name.databinding.ChildBinding mboundView;
// ~~~Abbreviation~~~
            this.mboundView.setValue_text(someVariableGetValue); // <-You! !!

error contents

error:Can't find symbol
            this.mboundView3.setValue_text(someVariableGetValue);
                            ^
symbol:Method setValue_text(String)

So, the function name generated by the ʻapp: snake_caseattribute is Build fails because it is different from the function name generated by`.

I feel like "Then ʻapp: camelCase & `," In that case, ** the name cannot be resolved on the xml file side, and an error occurs before the BindingImpl is generated **. Even on the writing side, switching between them is difficult in xml. (In my case)

Coping

As I wrote at the beginning.

child.xml


<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="valueText"
            type="String" />
    </data>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{valueText}"/>
</layout>

parent.xml


<Parent element omitted>
    <include
        layout="@layout/child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:valueText="@{someVariable}"/>
</Parent element omitted>

Let's change the definition to camelCase.

bonus

I don't think it's been stated so much, I also searched for a two-word variable name in the Official sample. It was camelCase. (ʻUserList`)

    <data>
        <import type="com.example.User"/>
        <import type="java.util.List"/>
        <variable name="user" type="User"/>
        <variable name="userList" type="List&lt;User>"/>
    </data>

There is a feeling that this area (then the id of resource is also camelCase?) Is not consistent even in the code provided by Google, so it seems that it will be a small topic from old times.

Recommended Posts

[Android] Variable of Data Binding is written in camelCase
[Android / Kotlin] UI summary
Android development link summary
[Android] Variable of Data Binding is written in camelCase
Notes in Android studio
Defeating Android Studio Part 3-6
Defeating Android Studio Part 1 & Part 2
RxJava (RxAndroid) + RxBus + Data Binding + room sample (based on Java1.7) on Android.
Data is not registered in Rails.
Create variable length binary data in Java
Spring Autowired is written in the constructor
What is the data structure of ActionText?
First Android development UI creation for busy people (In addition, Data Binding experience)
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[Order method] Set the order of data in Rails
[Java] Use of final in local variable declaration