Home > Blockchain >  How to open an activity by selecting an item from navigation drawer's menu? [KOTLIN]
How to open an activity by selecting an item from navigation drawer's menu? [KOTLIN]

Time:03-27

I am new on kotlin and android studio, but as a beginner I finished 40 percent of my first project. I want to open an activity by selecting an item from navigation drawer menu but trying to use others answers not fixed my problem, there was no clear answer to similar questions I saw, and all of them are in java language, I want to do this by KOTLIN language in my project. I hope my question be clear for you guys!

this are all my project related codes:

1- this is my navigation drawer menu items :

<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/itmSignOut"
android:title="@string/sign_out"/>
</group>
</menu>

2- this is the dashboard that navigation drawer is in that :

import af.azdreams.myconquer.databinding.ActivityDashboardBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout

class DashboardActivity : AppCompatActivity() {
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityDashboardBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)
    val drawerLayout = findViewById<DrawerLayout>(R.id.drawerLayout)
    val toolbar = findViewById<Toolbar>(R.id.toolbar)
    val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close)
    toggle.isDrawerIndicatorEnabled = true
    supportActionBar?.setDisplayShowTitleEnabled(false)
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
}
private var backPressedTime:Long = 0
private lateinit var backToast: Toast
override fun onBackPressed() {
    backToast = 
Toast.makeText(this,resources.getString(R.string.press_back_again_to_leave_the_app), 
Toast.LENGTH_SHORT)
    if (backPressedTime   2000 > System.currentTimeMillis()) {
        backToast.cancel()
        super.onBackPressed()
        return
    } else {
        backToast.show()
    }
    backPressedTime = System.currentTimeMillis()
}
}

3- this is the activity that i want to open after selecting from item menu :

import af.azdreams.myconquer.databinding.ActivitySignOutBinding
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.auth.FirebaseAuth

class SignOut : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var binding: ActivitySignOutBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivitySignOutBinding.inflate(layoutInflater)
    setContentView(R.layout.activity_sign_out)
    val view = binding.root
    setContentView(view)
    initData()
}
private fun initData(){
    auth = FirebaseAuth.getInstance()
    setUserEmail()
    clickListener()
}
private fun clickListener(){
    binding.btnSignOut.setOnClickListener{
        auth.signOut()
        startActivity(Intent(this,LoginActivity::class.java))
        finish()
    }
}
private fun getCurrentUserEmail():String? {
    return auth.currentUser?.email
}
@SuppressLint("SetTextI18n")
private fun setUserEmail(){
    binding.tvUserEmail.text = "Welcome " getCurrentUserEmail()
}
}

4- and this is the dashboard.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@ id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/Orange"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@ id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
    app:menu="@menu/nav_menu"
    android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

thank you for helping me on this problem. take care!

CodePudding user response:

What you're looking for is called an Intent: https://developer.android.com/reference/kotlin/android/content/Intent

In your case you have to override onOptionsItemSelected method within your first activity, then you can create an intent for navigating to the second activity.

Something like this

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when(item.itemId) {
        val intent = Intent(this,SecondActivity::class.java).apply
        startActivity(intent) 
        return true
    }
    return super.onOptionsItemSelected(item)
}
  • Related