Android passing a enum variable from one xml to an included xml file that consequently sends it to code

1.7k Views Asked by At

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.

0

There are 0 best solutions below