Unable to show menu icons in appbar - android

I want to show search icon in app bar for implementing search functionality.I have created menu resource file and inflate that in MainActivity but unable to see search icon.
Below is my code:
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="Item"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
</menu>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu,menu)
return true
}
}
Someone let me know why icon is not showing.

in your Activity's onCreate add this:
setSupportActionBar(binding.IdentifierOfYourToolBarInYourMainActivityXML)

Related

Can't show/hide menu items on Kotlin - Android Studio

my application has a menu of 3 options, and what I want is that according to a condition that I define, it shows only 2 options, or it shows 3.
I have tried all the options that I have found on google and here, but none have worked for me.
My idea is that as soon as the view is created, the menu already shows the options that it can show according to the condition. But as I have been able to achieve it, I added a button to simulate the event.
I would greatly appreciate your help.
Here I attach the code:
1) activity_home.xml (some part)
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="ActivarMenus"/>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:menu="#menu/home_menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
2) Aqui el archivo home_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item"
android:icon="#drawable/ic_baseline_more_vert_24"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle">
<menu>
<item
android:id="#+id/newRegister"
android:icon="#drawable/ic_baseline_add_24"
app:showAsAction="always"
android:visible="true"
android:title="Nueva Orden Swab" />
<item
android:id="#+id/newAnexoo"
android:icon="#drawable/ic_baseline_add_24"
app:showAsAction="always"
android:visible="true"
android:title="Nuevo Formulario Anexo O" />
<item
android:id="#+id/sync"
android:icon="#drawable/ic_baseline_sync_24"
app:showAsAction="always"
android:visible="true"
android:title="Sincronizar" />
</menu>
</item>
</menu>
3) And a part of the HomeActivity.kt
class HomeActivity : ActivityViewBinding<ActivityHomeBinding, HomeVM>() {
private var mainMenu: Menu? = null
private lateinit var spm : SharedPreferencesManager
private val adapter: OrderAdapter by inject()
override fun inflateLayout(layoutInflater: LayoutInflater) =
ActivityHomeBinding.inflate(layoutInflater)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
//MenuMain.findItem(R.id.newAnexoo).isVisible = true
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.home_menu, menu)
mainMenu = menu;
return true
}
fun ActivarMenus(view: View) {
mainMenu?.findItem(R.id.newRegister)?.isVisible = true
}
You should try this one it might be work.
if you want to change Menu Items at Run time You can use onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu){
if (//Your condition) {
menu.findItem(R.id.newRegister).setVisible(true);
}else {
menu.findItem(R.id.newRegister).setVisible(false);
}
return true;
}

ToolBar menu item click in a fragment (Kotlin)

In my app, i have an Activity, which has a FrameLayout in it. In this FrameLayout, there is a fragment, containing a ToolBar and a RecyclerView.
In this toolbar, i have a search button, which should start an Activity on item click. However, when i try to use onOptionsItemSelected, the apps gets built and installed succesfully, but when i try to tap that button in question, nothing happens. The Logcat, doesnt say anything either.
Can some points me what im doing wrong? Are there simpler or other easy ways to manage on ToolBar item clicks?
Fragment.kt
class FragmentTrack : Fragment() {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_track, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
topToolbar.setNavigationOnClickListener {
val dialog = FragmentBottomSheetDrawer()
dialog.show(childFragmentManager, dialog.tag)
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_toolbar, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
R.id.fsvSearch -> Toast.makeText(context, "Clicked search button", Toast.LENGTH_SHORT).show()
}
return true
}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.Design.NoActionBar">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/topToolbar"
android:background="#color/colorPrimaryDark"
app:navigationIcon="#drawable/ic_outline_menu_24"
app:popupTheme="#style/popupMenuThemeDark"
app:titleTextColor="#android:color/white"
app:title="Revo"
app:menu="#menu/menu_toolbar"
app:layout_scrollFlags="scroll|enterAlways" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvTracks"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/fsvSearch"
android:icon="#drawable/ic_search_24px"
android:title="#string/search"
app:showAsAction="always"/>
<item
android:id="#+id/fsvOrder"
android:icon="#drawable/ic_sort_24px"
android:title="#string/sort"
app:showAsAction="ifRoom">
<menu>
<group android:id="#+id/sortMenu" android:checkableBehavior="single">
<item
android:id="#+id/sortIncrease"
android:title="#string/increasing"/>
<item
android:id="#+id/sortDecrease"
android:title="#string/decreasing"/>
<item
android:id="#+id/sortMArtist"
android:title="#string/artist"/>
<item
android:id="#+id/sortAlbum"
android:title="#string/albums"/>
<item
android:id="#+id/sortYear"
android:title="#string/year"/>
<item
android:id="#+id/sortDate"
android:title="#string/date"/>
</group>
</menu>
</item>
<item
android:id="#+id/fsvGrid"
android:icon="#drawable/ic_grid_on_24px"
android:title="#string/grid"
app:showAsAction="ifRoom">
<menu>
<group android:id="#+id/gridMenu" android:checkableBehavior="single">
<item
android:id="#+id/gridOne"
android:title="1"/>
<item
android:id="#+id/gridTwo"
android:title="2"/>
<item
android:id="#+id/gridThree"
android:title="3"/>
<item
android:id="#+id/gridFour"
android:title="4"/>
</group>
</menu>
</item>
</menu>
I found a solution thanks to some external help. Its possible to work on the Toolbars item in an easier way.
In the onViewCreated method, we must add:
topToolbar.inflateMenu(R.menu.menu_toolbar)
topToolbar.setOnMenuItemClickListener {
when(it.itemId) {
R.id.fsvSearch -> //your code
}
true
}
Also, if the menu gets duplicated, remove the app:menu tag in the Toolbars xml
thanks Meltix for this, I've done a lot of research but most of the posts in SO are about Java and old fashioned ActionBar. There is not much info about material ToolBar and kotlin examples.
In my case I'm working on an old app developed mostly in Java but now developing new functionalities in Kotlin. In my fragment, I wasn't able to change the ActionBar's icons and behaviour (I wanted a particular ActionBar for my fragment). The solution I found is to hide the ActionBar and create a Toolbar inflating a custom menu inside. Also I added a back arrow button (thanks to John: here).
I've came to the conclusion (maybe I'm wrong) that you can't manage ActionBars from kotlin fragments, that's why you have to use toolbars. Here is my code in case it can help someone.
MyFragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// hide the old actionBar
(activity as AppCompatActivity).supportActionBar!!.hide()
// create the toolbar
val toolbar = binding?.myToolbar
// add the back arrow button, see function below
val resId = getResIdFromAttribute(toolbar.context, android.R.attr.homeAsUpIndicator)
toolbar.navigationIcon = ContextCompat.getDrawable(toolbar.context, resId)
// tapping on the arrow will pop the current fragment
toolbar.setNavigationOnClickListener { parentFragmentManager?.popBackStackImmediate() }
// change toolbar title
toolbar.title = "Some title"
// inflate a custom menu, see code below
toolbar.inflateMenu(R.menu.my_custom_menu)
toolbar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.infoButton -> someAction()
}
true
}
}
// get back arrow icon
#AnyRes
fun getResIdFromAttribute(context: Context, #AttrRes attr: Int): Int {
if (attr == 0) return 0
val typedValueAttr = TypedValue()
context.theme.resolveAttribute(attr, typedValueAttr, true)
return typedValueAttr.resourceId
}
my_fragment.xml
...
<androidx.appcompat.widget.Toolbar
android:id="#+id/myToolBar"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="visible"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="#color/azulOscuro"
>
</androidx.appcompat.widget.Toolbar>
my_custom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:jsmovil="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/infoButton"
android:icon="#drawable/ic_info_blanco"
android:title="#string/tutorial"
android:textColor="#color/blanco"
jsmovil:showAsAction="always"
/>
</menu>
Result

OptionsMenu disappears from fragment on orientation change

Icons in the option menu work just fine, but when you change orientation by rotating the screen the icons disappear.
class SingleRecipeFragment : Fragment() {
private lateinit var viewModel: SingleRecipeViewModel
private lateinit var viewModelFactory: SingleRecipeViewModelFactory
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentSingleRecipeBinding = DataBindingUtil.inflate(inflater,
R.layout.fragment_single_recipe, container, false)
val args =
SingleRecipeFragmentArgs.fromBundle(
requireArguments()
)
val application = requireNotNull(this.activity).application
viewModelFactory =
SingleRecipeViewModelFactory(
args.recipeIndex, application
)
viewModel = ViewModelProvider(this,viewModelFactory)
.get(SingleRecipeViewModel::class.java)
binding.singleRecipeViewModel = viewModel
binding.lifecycleOwner = this
viewModel.curRecipe.observe(viewLifecycleOwner, Observer {
(requireActivity() as MainActivity).toolbar.title = it?.title
})
//Toast.makeText(context, "Recipe Number: ${args.recipeIndex}",Toast.LENGTH_LONG).show()
setHasOptionsMenu(true)
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.single_recipe_menu, menu)
viewModel.curRecipe.observe(viewLifecycleOwner, Observer {
val favIconDrawable: Int = if (it!!.favorite) R.drawable.ic_baseline_star_filled_24
else R.drawable.ic_baseline_star_border_24
menu.findItem(R.id.favorite).setIcon(favIconDrawable)
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> viewModel.shareSuccess(this.requireActivity())
R.id.favorite -> viewModel.toggleFavorite()
}
return super.onOptionsItemSelected(item)
}
}
Here's the menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:enabled="true"
android:icon="#drawable/ic_baseline_share_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorite"
android:enabled="true"
android:icon="#drawable/ic_baseline_star_border_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
This fragment is launched from another fragment by NavigationUI which passes in the recipeID value. I've tried using setHasOptionsMenu(true) in the onResume of this fragment but it has no effect. Any idea why the optionsMenu is not persistent?
It seems that using setSupportActionBar(toolbar) in the onCreate() method of the MainActivity fixes the problem with the optionsMenu not being persistent. However, this breaks the method I was using to display the title on fragment windows.
The toolbar belongs to the Activity, not the fragment. So when the view is recreated after the orientation change the toolbar is being set to the app title. This doesn't happen when the fragment is first loaded since the title was being set by the fragment's onCreate(). So the solution to that was to move the custom titles to onResume() of the fragment so that it is reset ever time the view is shown on the screen.
I'm setting the title in the fragment like this, because if I let the Navigation Controller do it using the labels in navigation.xml I get a strange behavior when the fragment loads up where the custom toolbar layout being replace by the new fragment title kind of flashes out to the right.
So the boiled-down code for this solution is:
Fragment:
class SingleRecipeFragment : Fragment() {
override fun onCreateView(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onResume() {
super.onResume()
/** This will reset your title every time the fragment is redrawn **/
(requireActivity() as MainActivity).toolbar.title = "Some Custom Title"
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.single_recipe_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> someMethodYouWantToRun()
R.id.favorite -> someOtherMethod()
}
return super.onOptionsItemSelected(item)
}
}
In onCreate() of your Activity, call:
setSupportActionBar(toolbar)
And here's the menu layout resource file being inflated for the options menu (no change from what's in the question):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:enabled="true"
android:icon="#drawable/ic_baseline_share_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorite"
android:enabled="true"
android:icon="#drawable/ic_baseline_star_border_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>

How to make custom toolbar menu visible for my android app?

I have prepared the custom toolbar toolbar.xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:layout="http://schemas.android.com/tools"
android:id="#+id/my_toolbar1"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#BCBCDD"
android:elevation="4dp"
app:logo="#drawable/ic_baseline_menu_24"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidx.appcompat.widget.Toolbar>
Now I have created a menu directory, and in that toolbar_menu.xml file as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_share"
android:title="Share"/>
<item
android:id="#+id/menu_settings"
app:showAsAction="always"
android:icon="#drawable/ic_baseline_settings_24"
android:title="Settings"/>
<item
android:id="#+id/menu_exit"
android:title="Exit"/>
</menu>
Now I have included this toolbar layout in my main activity. xml with <include/> tag.
After all this implementation I have set the toolbar in MainActivity.kt which is as below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(my_toolbar.findViewById(R.id.my_toolbar1)) //Set the Action Bar -> toolbar
val apiService : ApiService = ApiClient.getClient()
coronaRepository =
CoronaDetailsRepository(
apiService
)
viewModel = getViewModel(1,1)
viewModel.coronaDetails.observe(this, Observer {
bindUI(it)
})
viewModel.networkState.observe(this, Observer {
loader.visibility = if (it == NetworkState.LOADING) View.VISIBLE else View.GONE
})
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.toolbar_menu, menu) // Used Menu Inflater to inflate the layout
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.menu_share -> Toast.makeText(applicationContext, "Share", Toast.LENGTH_SHORT).show()
R.id.menu_settings -> Toast.makeText(applicationContext, "Settings", Toast.LENGTH_SHORT).show()
R.id.menu_exit -> Toast.makeText(applicationContext, "Exit", Toast.LENGTH_SHORT).show()
}
return super.onOptionsItemSelected(item)
}
My toolbar is appearing on the main screen when app runs but the menu is not visible. I am wondering that what is wrong going on in my code??
Inside onCreate
..
setSupportActionBar(findViewById(R.id.my_toolbar1))
..
Inside onCreateOptionsMenu
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return super.onCreateOptionsMenu(menu)
}

Navigation drawer menu item click no response

I am developing an android app and am new to programming. I have extended the base drawer activity to all activity. But after I clicked the menu button of the drawer in the activities that extend drawer activity, nothing happened. Below is my code in the base drawer activity.
open class DrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.requestWindowFeature(Window.FEATURE_NO_TITLE)
this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_drawer)
setSupportActionBar(toolbar)
nav_view.setNavigationItemSelectedListener(this)
pageToGo = 1
println("PageToGo on start activity: $pageToGo")
}
override fun onBackPressed() {
println("Back is pressed")
if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
drawer_layout.closeDrawer(GravityCompat.END)
} else {
finishAffinity()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.drawer, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_show_stat -> {
println("I am clicked")
}
R.id.nav_restart_game -> {
}
R.id.nav_achievements -> {
}
R.id.nav_settings -> {
}
R.id.nav_about_us -> {
}
R.id.nav_contact_us -> {
}
}
drawer_layout.closeDrawer(GravityCompat.END)
return true
}
override fun setContentView(layoutResID:Int) {
val fullLayout: DrawerLayout
val actContent: FrameLayout
fullLayout = layoutInflater.inflate(R.layout.activity_drawer, null) as DrawerLayout
actContent = fullLayout.findViewById(R.id.act_content) as FrameLayout
layoutInflater.inflate(layoutResID, actContent, true)
super.setContentView(fullLayout)
}
Below is my drawer_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
layout="#layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/act_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer"
app:menu="#menu/activity_drawer_drawer">
</android.support.design.widget.NavigationView>
Below is my menu code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_show_stat"
android:icon="#drawable/ic_menu_camera"
android:title="#string/nav_show_stat" />
<item
android:id="#+id/nav_restart_game"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/nav_restart" />
<item
android:id="#+id/nav_achievements"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/nav_achievements" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_manage"
android:title="#string/nav_settings" />
</group>
<item android:title="#string/nav_our_company">
<menu>
<item
android:id="#+id/nav_about_us"
android:icon="#drawable/ic_menu_share"
android:title="#string/nav_about_us" />
<item
android:id="#+id/nav_contact_us"
android:icon="#drawable/ic_menu_send"
android:title="#string/nav_contact_us" />
</menu>
</item>
Below is my code in main activity
class MainActivity : DrawerActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
load()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
beginAdventure()
main_layout.setOnTouchListener(object : OnSwipeTouchListener(applicationContext) {
override fun onSwipeLeft() {
drawer_layout.openDrawer(GravityCompat.END)
}
})
}
private fun beginAdventure() {
start_game_btn.setSafeOnClickListener {
nextPage("ChapterOneActOneActivity", 1)
}
}
}
I know I am missing something important. But I just don't know what it is. The onNavigationItemSelected seems not working because when I click on R.id.nav_show_stat, "I am clicked" never gets printed. Please kindly let me know what I should do to make the drawer buttons work in all activity. Thank you in advance.
Edit2: my drawer in main activity
https://imgur.com/a/ofGzncZ
So when I click on Show Statistics, the "I am clicked" is never printed. How do I handle the click event to do what I want?
No need to use the below given code separately :-
override fun setContentView(layoutResID:Int) {
val fullLayout: DrawerLayout
val actContent: FrameLayout
fullLayout = layoutInflater.inflate(R.layout.activity_drawer, null) as DrawerLayout
actContent = fullLayout.findViewById(R.id.act_content) as FrameLayout
layoutInflater.inflate(layoutResID, actContent, true)
super.setContentView(fullLayout)
}
just add :-
setContentView(R.layout.your_layout_filename);
i think there is no other issue in your code

Categories

Resources