Reputation: 51
i'm stuck in my application because i have to do a fragment with the navigation drawer in kotlin.. can anyone help? I have search a lot of things in web but i didn't find anything yet... Down here is two items that i have do in main activity and i want that is present in the fragment too.
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
Upvotes: 2
Views: 3612
Reputation: 1
i have a wikipedia project and this is code it you should know that this is a return function and end code you set true
//here is for set the drawer option
val actionBarDrawerToggle = ActionBarDrawerToggle(
this,
binding.drawerLayoutMain,
binding.toolbarMain,
R.string.openDrawer,
R.string.closeDrawer
)
binding.drawerLayoutMain.addDrawerListener(actionBarDrawerToggle)
//this code is for hamburger icon for drawer items
actionBarDrawerToggle.syncState()
//here is for navigation drawer show to user
binding.navigationViewMain.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.writer_menu -> {
val intent = Intent(this, writer_activity::class.java)
startActivity(intent)
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
R.id.photographer_menu -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.add(R.id.fragment_container_view, FragmentPhotographer())
transaction.addToBackStack(null)
transaction.commit()
//item checkable is true
binding.navigationViewMain.menu.getItem(1).isChecked = true
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
R.id.video_menu -> {
//show a snakbar to user
Snackbar
.make(binding.root,"You clicked on video maker" , Snackbar.LENGTH_LONG)
.setAction("Ok"){
Toast.makeText(this, "You clicked ok of SnakBar ", Toast.LENGTH_SHORT).show()
}
.setActionTextColor(ContextCompat.getColor(this , R.color.md_theme_onPrimary))
.setBackgroundTint(ContextCompat.getColor(this, R.color.md_theme_primaryContainer))
.show()
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
R.id.translator_menu -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.add(R.id.fragment_container_view,translatorFragment())
transaction.addToBackStack(null)
transaction.commit()
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
//---------------menu group
R.id.wikipedia_menu -> {
val url = "https://en.wikipedia.org/wiki/Main_Page"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
R.id.wikimedia_menu -> {
val url = "https://www.wikimedia.org/"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
binding.drawerLayoutMain.closeDrawer(binding.navigationViewMain)
}
}
true
}
Upvotes: 0
Reputation: 1425
You can use this to replace your frame with HomeFragment() mainDisplayFrame should be replace by your FrameID
var fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.mainDisplayFrame,HomeFragment()).commit()
mDrawerLayout!!.closeDrawers()
Upvotes: 2
Reputation: 2094
You can download the source link from here(Navigation Drawer In Android Kotlin)
MainActivity.kt:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init() {
tv_home.setText("Home Tab")
tv_title.setText("Home")
ll_about.setOnClickListener(this)
ll_home.setOnClickListener(this)
ll_bookmark.setOnClickListener(this)
ll_help.setOnClickListener(this)
rl_menu.setOnClickListener(this)
rl_header.setOnClickListener(this)
tv_link.setLinkTextColor(Color.parseColor("#000000"));
Linkify.addLinks(tv_link, Linkify.ALL)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.ll_home -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Home Tab")
tv_title.setText("Home")
}
R.id.ll_about -> {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://deepshikhapuri.blogspot.in/"))
startActivity(browserIntent)
}
R.id.ll_help -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Help Tab")
tv_title.setText("Help")
}
R.id.ll_bookmark -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Bookmark Tab")
tv_title.setText("Bookmark")
}
R.id.rl_header -> {
}
R.id.rl_menu -> {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
drawer_layout.openDrawer(GravityCompat.START)
}
}
}
}
Thanks!
Upvotes: 1
Reputation: 51
Is something like this? @NongthonbamTonthoi
class NavViewFragment : AppCompatActivity(),
NavigationView.OnNavigationItemSelectedListener {
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){
supportFragmentManager.inTransaction { add(frameId, fragment) }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_view_fragment)
setSupportActionBar(toolbar)
nav_view.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.nav_view, 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.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
val fragmentTransaction = beginTransaction()
fragmentTransaction.func()
fragmentTransaction.commit()
}
}
Upvotes: 0