Navigation View Menu Item Doesn't respond to any Click - android

i want to know what is wrong with my code as there is no any responding to my clicks on the menu item
class Main : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(mainToolbar)
supportActionBar?.title = ""
val toolbarToggle = ActionBarDrawerToggle(this,DrawerLayout,mainToolbar,R.string.drawer_open,R.string.drawer_close)
DrawerLayout.addDrawerListener(toolbarToggle)
toolbarToggle.syncState()
mainNavigView.setNavigationItemSelectedListener(this)
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var titleT = item.title
Toast.makeText(this,titleT, Toast.LENGTH_LONG).show()
return true
}
}
this is xml code for navigation and drawer
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/DrawerLayout"
android:background="#color/spark_bg"
tools:context=".Main">
<com.google.android.material.navigation.NavigationView
android:id="#+id/mainNavigView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigationheader"
app:menu="#menu/navigationmenu"
/>
this is menu item code that contains id for the item i wanted to press
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/myOrdersMI"
android:icon="#drawable/ic_list"
android:title="My Orders"
/>
</menu>

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no <layout_gravity>
<androidx.drawerlayout.widget.DrawerLayout
...>
<!-- main content goes here -->
<!-- NavigationView -->
<com.google.android.material.navigation.NavigationView
android:layout_gravity="start|left"
../>
</androidx.drawerlayout.widget.DrawerLayout>

Related

Unable to select any of the items in the menu

I have to get the toast when i click on the profile item and move to other activity when i click on the SignOut items. but nothing is happening and unable to click too.
MainActivity code:
where i have all the functionalities to set up the nav and its menu items
class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener { // to make items on menu selectable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpActionBar()
// setting up the nav_view
nav_view.setNavigationItemSelectedListener(this)
}
private fun setUpActionBar() {
setSupportActionBar(toolbar_main_activity)
title = "Thinking"
toolbar_main_activity.setNavigationIcon(R.drawable.ic_baseline_menu_24)
toolbar_main_activity.setNavigationOnClickListener { toggleDrawer() }
}
private fun toggleDrawer() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
drawer_layout.openDrawer(GravityCompat.START)
}
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
doubleBackToExit()
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_my_profile -> {
Toast.makeText(this, "My profile", Toast.LENGTH_LONG).show()
}
R.id.nav_sign_out -> {
FirebaseAuth.getInstance().signOut()
startActivity(Intent(this, IntroActivity::class.java))
finish()
}
else -> {
drawer_layout.closeDrawer(GravityCompat.START)
}
}
return true
}
}
Menu Code:
I have used two items
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_my_profile"
android:icon="#drawable/ic_nav_user"
android:title="My profile" />
<item
android:id="#+id/nav_sign_out"
android:icon="#drawable/ic_nav_sign_out"
android:title="Sign out" />
</group>
</menu>
XML Activity: I have included another activity to display upon this activity
<?xml version="1.0" encoding="utf-8"?>
<!-- Drawer Layout creation-->
<androidx.drawerlayout.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:theme="#style/Custom_Theme"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
<include layout="#layout/main_content" />
</androidx.drawerlayout.widget.DrawerLayout>

Using navigation component with bottom navigation bar by using FragmentContainerView tag

I have already looked this question: FragmentContainerView using findNavController about this problem. But however I stiil couldn't solve the problem. It never opens other fragments. I thought maybe it is because of view binding but I tried to do it again without using view binding. It doesn't open still.
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
private lateinit var navHostFragment: NavHostFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
binding.bottomNavigationView.setupWithNavController(navController)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.home -> {
navHostFragment.navController.popBackStack()
return true
}
}
return super.onOptionsItemSelected(item)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/main_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph_main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="56dp"
app:menu="#menu/menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_graph_main
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph_main"
app:startDestination="#id/fragmentLibrary">
<fragment
android:id="#+id/fragmentLibrary"
android:name="com.project.biosec.FragmentLibrary"
android:label="fragment_library"
tools:layout="#layout/fragment_library" />
<fragment
android:id="#+id/fragmentTransaction"
android:name="com.project.biosec.FragmentTransaction"
android:label="fragment_transaction"
tools:layout="#layout/fragment_transaction" />
<fragment
android:id="#+id/fragmentAccount"
android:name="com.project.biosec.FragmentAccount"
android:label="fragment_account"
tools:layout="#layout/fragment_account" >
<action
android:id="#+id/action_fragmentAccount_to_changeSignatureFragment"
app:destination="#id/changeSignatureFragment" />
<action
android:id="#+id/action_fragmentAccount_to_securityFragment"
app:destination="#id/securityFragment" />
<action
android:id="#+id/action_fragmentAccount_to_helpFragment"
app:destination="#id/helpFragment" />
<action
android:id="#+id/action_fragmentAccount_to_termsFragment"
app:destination="#id/termsFragment" />
</fragment>
<fragment
android:id="#+id/changeSignatureFragment"
android:name="com.project.biosec.ChangeSignatureFragment"
android:label="fragment_change_signature"
tools:layout="#layout/fragment_change_signature" />
<fragment
android:id="#+id/securityFragment"
android:name="com.project.biosec.SecurityFragment"
android:label="fragment_security"
tools:layout="#layout/fragment_security" />
<fragment
android:id="#+id/helpFragment"
android:name="com.project.biosec.HelpFragment"
android:label="fragment_help"
tools:layout="#layout/fragment_help" />
<fragment
android:id="#+id/termsFragment"
android:name="com.project.biosec.TermsFragment"
android:label="fragment_terms"
tools:layout="#layout/fragment_terms" />
</navigation>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/feed_fragment"
android:icon="#drawable/ic_library"
android:title="#string/library" />
<item
android:id="#+id/messages_fragment"
android:icon="#drawable/ic_assignment"
android:title="#string/transactions" />
<item
android:id="#+id/profile_fragment"
android:icon="#drawable/ic_account"
android:title="#string/account" />
</menu>
As per the Setting up bottom navigation guide:
Note: Setting up bottom navigation requires that you also set up your navigation graph and menu xml as described in Tie destinations to menu items.
That section specifically states that the android:id of your destination in your navigation graph XML file needs to match the android:id of the menu item in your menu XML file.
In your case, your navigation XML uses android:id="#+id/fragmentLibrary", android:id="#+id/fragmentTransaction", and android:id="#+id/fragmentAccount", so your menu items should change to use those same IDs.

Android Toolbar pulls down - Navigation component

I am trying to transition an app that I have previously made to use Navigation component with fragments instead of activities ( so I'm converting my activities to fragments so they work with the component and also because it's nicer this way) and I have a problem with the Toolbar always getting pulled down a little bit without any aparent reason. I tried changing the layout roots from LinearLayout to ConstraintLayout, back again, fidgeting with android:fitsSystemWindows both for the activity layout and for the fragment one as true or false, also with views layout_height but no luck so far...
The activity looks like this:
here are my files:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val baseAppState by inject<BaseAppStateManager>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.myNavHostFragment) as NavHostFragment
val navController = navHostFragment.navController
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.myNavHostFragment)
return navController.navigateUp()
}
}
fragment_devices.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:orientation="vertical">
<include
android:id="#+id/dashboard_status"
layout="#layout/dashboard_phone_status"
android:layout_width="match_parent"
android:layout_height="160dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/devices_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
DevicesFragment.kt
class DevicesFragment : Fragment() {
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, sis: Bundle?): View {
binding = FragmentDevicesBinding.inflate(inflater, container, false)
phoneStatusScreen = PhoneStatusScreenManager(binding.dashboardStatus)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
observeLiveData()
...viewmodel calls...
historyAdapter = DevicesHistoryAdapter(onDeviceClicked = ::onDeviceClicked)
setupRecyclerView(historyAdapter)
}
Sometimes the toolbar looks okay when I open the app but as soon as I navigate to another fragment by clicking on an element from the RecyclerView, when I click on the back arrow to navigate up, the toolbar is like you see in the screenshot below.
Also, here is the application theme:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Theme customization. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>

Kotlin Android Navigation Drawer item onclick listener does not working

I know this already asked many times, but in my case, I still don't get it.
So I have a navigation drawer that set up in the MainActivity.kt (My app has many fragment that runs on this Activity).
MainActivity.kt
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var drawerLayout: DrawerLayout
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
drawerLayout = binding.drawerLayout
//Get navigation controller of this App
val navController = this.findNavController(R.id.myNavHostFragment)
//Lock the Nav drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
NavigationUI.setupWithNavController(binding.navView, navController)
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_logout -> {
Toast.makeText(this, "Publication", Toast.LENGTH_SHORT).show()
Log.i("MainActivity", "Sign out clicked!")
}
R.id.nav_messages -> {
Toast.makeText(this, "Publication", Toast.LENGTH_SHORT).show()
}
}
binding.drawerLayout.closeDrawer(GravityCompat.START)
return true
}
// Set up the back button on action bar
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.myNavHostFragment)
return NavigationUI.navigateUp(navController, drawerLayout)
}
}
This is my Activity_main.xml
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
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="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph = "#navigation/navigation"
app:defaultNavHost = "true"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
This is the menu layout for the navigation drawer:
nav_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_person"
android:title="Profile"/>
<item
android:id="#+id/nav_messages"
android:icon="#drawable/ic_people"
android:title="Messages"/>
</group>
<item android:title="Account Settings">
<menu>
<item
android:id="#+id/nav_logout"
android:title="Sign Out"/>
</menu>
</item>
</menu>
This is how the navigation drawer looks like.
Am I missing something ? If there's anything unclear let me know.
You should use setNavigationItemSelectedListener.
Set a listener that will be notified when a menu item is selected.
navigationViewOBJ.setNavigationItemSelectedListener(this)
For DataBinding You should use
binding.navView.setNavigationItemSelectedListener(this)
I was facing the same issue and I changed my code in this manner to get Toast when Menuitems were select.
I removed NavigationView.OnNavigationItemSelectedListener this from
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener
created a variable in MainActivity class in MianActivity.kt
private lateinit var navView : NavigationView
assigned navView variable in override fun onCreate function like this by finding navigation view id like this
navView = findViewById(R.id.nav_menu)
which in your case is in activity_main.xml under this Section
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_drawer_menu"/>
Then I implemented this function for onclick select
navView.setNavigationItemSelectedListener {}
I wrote switch case in this function same as your code and now it is working for me fine.

How to add a click listener to Menu Item in Kotlin

I want to create a Bottom Navigation Bar. I used android BottomNavigationView to create the UI but I don't know how to add an OnClick listener to the items in Menu.
I searched on google and found some articles, but all of them were using a Toolbar element. I don't know about how to add that and what it was doing.
This is my Navigation bar code which I am including in the main activity
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:layout_width="match_parent" android:id="#+id/bottom_nav"
android:background="#drawable/gradient_theme"
android:layout_height="wrap_content" android:layout_gravity="bottom">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:id="#+id/menuBar"
design:menu="#menu/menu_bar"
design:itemIconTint="#android:color/darker_gray"/>
</RelativeLayout>
</FrameLayout>
This is my MainActivity.kt in which i want to set Listeners
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
beautifyLayout(this, window)
setSupportActionBar(toolbar)
testButton.setOnClickListener {
val intent= Intent(this,AccountActivity::class.java)
finish()
startActivity(intent)
}
}
}
This is menu_bar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/btn1" android:title="" android:icon="#drawable/ic_feed"/>
<item android:id="#+id/btn2" android:title="" android:icon="#drawable/ic_chat_bubble_black_24dp"/>
<item android:id="#+id/btn3" android:title="" android:icon="#drawable/ic_search_black_24dp"/>
<item android:id="#+id/btn4" android:title="" android:icon="#drawable/ic_menu_black_24dp" />
</menu>
The UI is working perfectly ,it only requires some Listeners. I want if possible to use only BottomNavigationView.
This is how you set the listener:
val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.btn1 -> {
// put your code here
return#OnNavigationItemSelectedListener true
}
R.id.btn2 -> {
// put your code here
return#OnNavigationItemSelectedListener true
}
R.id.btn3 -> {
// put your code here
return#OnNavigationItemSelectedListener true
}
R.id.btn4 -> {
// put your code here
return#OnNavigationItemSelectedListener true
}
}
false
}
menuBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
If menuBar is inside your activity's layout it does not need initialization.
If not, you must use findViewById() to initialize it.

Categories

Resources