I want to add a clickable icon/button to the left of the title in the action bar . How to do it? Following is the code with which I have added search and setting icon to the act
I always like to put a AppBarLayout that I can control the way the toolbar appears
That way a can put more elements and arrange them like I want. In this case insert a left icon to close the activity by a fragment.
So in the fragment a do this
//That is in the Fragment
setupToolbar(R.id.toolbarAddPlan,getString(R.string.addPlanTour),true)
//It is in na Kotlin Extansion file.
inline fun Fragment.setupToolbar(@IdRes id: Int, title:String?= null, upNavigation: Boolean = false) : ActionBar {
val activityCompat = activity as AppCompatActivity
val toolbar = activity?.findViewById(id)
activityCompat.setSupportActionBar(toolbar)
if(title != null){
activityCompat.supportActionBar?.title = title
}
activityCompat.supportActionBar?.setHomeButtonEnabled(true)
return activityCompat.supportActionBar!!
}
The result will be something like this:
The action "SALVAR" i put via inflat menu
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
super.onCreateOptionsMenu(menu, inflater)
inflater?.inflate(R.menu.menu_add_plan_tour,menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId){
R.id.menuPlanTourSave ->{
true
}else ->return super.onOptionsItemSelected(item)
}
}
Menu XML layout src/menu/menu_add_plan_tour.xml
And do not forget to use setHasOptionsMenu(true) to enable the menu.