Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding with srcCompat

I'm using the new vector drawable support in Support Lib v23.2 with app:srcCompat & trying to set its drawable via data binding.

<layout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">  <data>     <variable         name="mediaPojo"         type="in.ishaan.pika.data_binding.MediaPojo"/> </data>  <RelativeLayout     android:background="@color/black"     android:layout_width="match_parent"     android:layout_height="match_parent">      <VideoView         ... />      <ImageView         ...         app:srcCompat="@{mediaPojo.isPlaying ? @drawable/ic_pause_24dp : @drawable/ic_play_arrow_24dp}"     />      <ProgressBar         .../> </RelativeLayout> </layout> 

On trying to build, studio throws:

Error:(33, 30) Cannot find the setter for attribute 'app:srcCompat' with parameter type android.graphics.drawable.Drawable.

like image 529
Ishaan Garg Avatar asked Mar 03 '16 08:03

Ishaan Garg


People also ask

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

Is data binding good in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.


2 Answers

You can simply use android:src attribute instead compat attribute when you set vector resource by DataBinding.

DataBinding library generates class that execute setImageResource method at runtime.

<ImageView         ...         android:src="@{@drawable/your_drawable}" /> 

According to http://android-developers.blogspot.com/2016/02/android-support-library-232.html setImageResource method can be used at runtime on older system versions without any additional changes.

If you would like to use app:srcCompat attribute. You must define @BindingMethods annotation which connects attribute with appropriate setter from ImageView. For example in your Activity or Fragment add.

@BindingMethods({     @BindingMethod(type = android.widget.ImageView.class,             attribute = "app:srcCompat",             method = "setImageDrawable") }) public class MainActivity extends AppCompatActivity {    // your activity body here  } 
like image 52
lukjar Avatar answered Oct 04 '22 09:10

lukjar


You may have to resort to using a binding adapter with a method signature similar to the following:

@BindingAdapter("app:srcCompat") public static void bindSrcCompat(ImageView imageView, Drawable drawable){     // Your setter code goes here, like setDrawable or similar } 

Here is the reference: http://developer.android.com/reference/android/databinding/BindingAdapter.html

like image 40
SeptimusX75 Avatar answered Oct 04 '22 11:10

SeptimusX75