Android Data Binding SetSupportActionBar
Make project in Android Studio then you can use below code for accessing toolbar:
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(binding.toolbar);
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/>