Igor Klimov Igor Klimov - 2 months ago 36
Android Question

Android Data Binding SetSupportActionBar

Hi I wonder if there is an xml tag for this function using Android Data Binding Library or how to achieve this without findViewById() method

Thank You

Answer

You can access the instance of the toolbar using views by id function

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

then on your onCreate() method do the following

ActivityGalleryBinding binding = DataBindingUtil
    .setContentView(this, R.layout.activity_gallery);
binding.setViewModel(new GalleryModel(this));
//set it like this
setSupportActionBar(binding.<location>.<of>.<your>.toolbar);

if your toolbar is inside other xml component (referenced with <include/>) you still can access it as long as you provide and @id to the <include/>