androidandroid-layoutandroid-xmlandroid-binding-adapterbindingadapter

Customising Androids XML Layout Field contentInsetStartWithNavigation


I have my AppBarLayout with Toolbar in a separate file and included it in my activity layouts. On some screens the layout needs to have a different dimension on the fields contentInsetStart and contentInsetStartWithNavigation.

The fields contentInsetStart and contentInsetStartWithNavigation don't even build.

How I include the AppBar and set the custom properties

<include
    android:id="@+id/include_app_bar_layout"
    layout="@layout/view_app_bar_layout"
    app:toolbarTitle="@{@string/custom_title}"
    app:toolbarContentInsetStart="@{@dimen/customInsetStart}"
    app:toolbarCISWN="@{@dimen/customInsetStartWithNavigation}"/>

The properties are dp values

<dimen name="customInsetStart">92dp</dimen>
<dimen name="customInsetStartWithNavigation">92dp</dimen>

The AppBarLayout with a Toolbar and some variables

<?xml version="1.0" encoding="utf-8"?>
<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>

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

        <variable
            name="toolbarContentInsetStart"
            type="java.lang.Float" />

        <variable
            name="toolbarCISWN"
            type="Integer" />

        <variable
            name="appBarLayoutElevation"
            type="java.lang.Float" />

        <variable
            name="appBarLayoutBackground"
            type="Integer" />

    </data>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@{appBarLayoutBackground}"
        app:elevation="@{appBarLayoutElevation}"
        tools:ignore="RtlHardcoded,RtlSymmetry">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.MyStyle.Toolbar"
            app:title="@{toolbarTitle}"
            app:contentInsetStart="@{toolbarContentInsetStart}"
            app:contentInsetStartWithNavigation="@{toolbarCISWN}"
            />

    </com.google.android.material.appbar.AppBarLayout>
</layout>

The Problem

If I define the variable toolbarCISWN as Integer like above then I get a build error that says

Cannot find setter for MyActivityLayoutFileBinding app:toolbarContentInsetStartWithNavigation that accepts parameter type 'float'.

If I set the variable type to Float or java.lang.Float (like the elevation variable) then I get the error in the xml file that app:contentInsetStartWithNavigation is not of type Float

Cannot find a setter for androidx.appcompat.widget.Toolbar app:contentInsetStartWithNavigation that accepts parameter type 'java.lang.Float'


Solution

  • The solution for me was to make the AppBarLayout a custom view class. I then removed all the variables from the xml and made custom attributes in the attr.xml file. Now I had full control from the "host" xml. I now do not "include" a layout file but just use the custom view in the "host" xml like an i.e. TextView. For additional features I added binding adapters.