0

I'm trying to reuse a xml file which I include in my main_layout.xml. In the code of my activity, I check for a variable "positionType" and depending on that variable I run different codes.

PositionType.java

public enum PositionType {
    play_top, grow_top, love_top
}

main_layout.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">


    <data>
        <import type="com.xxx.models.PositionType"/>
        <variable name="positionType"  type="com.xxx.models.PositionType"/>
    </data>

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

            <include
                android:id="@+id/circle_view_play_top"
                app:positionType="${PositionType.play_top}"
                layout="@layout/include_circle"
                />

[...]

include_circle.xml

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="positionTypeStr" type="com.xxx.models.PositionType"/>
    </data>

    <merge>
        <com.xxx.components.DeformedCircleView
            app:positionTypeStr="@{positionTypeStr}"
            android:clickable="true"
            android:layout_width="@dimen/home_small_circle_view"
            android:layout_height="@dimen/home_small_circle_view"/>
    </merge>

</layout>

DeformedCircleView.xml

[...]

protected PositionType positionType;

public DeformedCircleView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //I should receive here the PositionType.play_top (not sure how!) or in the setter
}

public PositionType getPositionType() {
    return this.positionType;
}

public void setPositionType(PositionType positionType) { 
    //I should receive here the PositionType.play_top or in the constructor
    this.positionType = positionType;
}



[...]

Problem is that I never receive in my DeformedCircleView constructor or setPositionType setter the value PositionType.play_top (that I'm sending from main_layout.xml). What I'm doing wrong?

By the way, do you have any idea of how to pass other parameters from main_layout.xml to include_circle.xml like @id and the layout_with/height resource?

Thanks in advance.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Eduardo Pinheiro
  • 3,409
  • 3
  • 30
  • 39
  • Is it a typo in your question or are you really using `app:positionTypeStr` and `setPositionType()`? I would expect that the `setPositionTypeStr()` method to be called with that attribute. You can pass any variable you want from an outer layout to an included layout by using `app:whateverVar="@{someValue}"`. You must be explicit about all variables. I'm not sure you'd want to pass layout parameters to data binding as they aren't supported by default. It can be added, though, so search for the stack overflow question on that. – George Mount Jul 24 '17 at 21:15
  • Hi George, it's a typo. I have it right in the code. Thanks for your comment. – Eduardo Pinheiro Jul 25 '17 at 10:50
  • In that case, the only error I see is this: `app:positionType="${PositionType.play_top}"` It should read `app:positionType="@{PositionType.play_top}". Normally you would notice this immediately because you would see an error that says the attribute `app:positionType` doesn't exist. If you had to add that attribute in attrs.xml then you did see that problem. – George Mount Jul 25 '17 at 14:16
  • It makes sense. I tried and still not work. The value that is passed is null. Thanks for your help though. – Eduardo Pinheiro Jul 27 '17 at 09:53

0 Answers0