0

I'm using a custom view, com.deadballapp.deadball.ui.pitch.PitchLiveView, but my binding class is unable to access this view. The binding class is able to access cl_pitch_live

PitchLiveView isn't inflated, it's a class that extends View which I'm using to draw to the Canvas so any solution to binding to a custom view I've seen here, here and here don't seem to apply

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

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/cl_pitch_live"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/pitch_green">

        <view
            android:id="@+id/view_pitch_live"
            class="com.deadballapp.deadball.ui.pitch.PitchLiveView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/nav_header_vertical_spacing"
            android:layout_marginBottom="@dimen/btm_sheet_peek_height" />

        <include layout="@layout/bottom_sheet_pitch_live"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

Any ideas?

Strokes
  • 157
  • 7
  • 23
  • 1
    first thing `view` should be `View`. – ADM Mar 07 '21 at 11:18
  • Using `View` then that would refer to the `View` base class? They're referring to a custom class, like they do here https://developer.android.com/guide/topics/ui/custom-components#modifying. – Henry Twist Mar 07 '21 at 12:26
  • Can you edit your question with what actually happens when you attempt this? Compile time error, runtime errors? I have tested this and it works fine for me. – Henry Twist Mar 07 '21 at 18:03

1 Answers1

0

This is the alternate method to define custom view in layout xml.

 <com.deadballapp.deadball.ui.pitch.PitchLiveView
     android:id="@+id/view_pitch_live"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="@dimen/nav_header_vertical_spacing"
     android:layout_marginBottom="@dimen/btm_sheet_peek_height" />

You can bind this as other views, either by using View Binding or findViewById

Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
  • Do you have a source for why the `view` tag isn't supported with data binding? – Henry Twist Mar 07 '21 at 12:23
  • I had thought this way using `view` tag works with custom view defined as inner class as in the Notepad example. Updated the answer. Just for [reference](https://developer.android.com/codelabs/advanced-andoid-kotlin-training-custom-views#0) – Varsha Kulkarni Mar 09 '21 at 02:23
  • When I tried referencing it in my example, it did show `unresolved reference` error with data binding/ view binding. – Varsha Kulkarni Mar 09 '21 at 02:32