Skip to content Skip to sidebar Skip to footer

Fragment In Kotlin

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

Solution 1:

You can download the source link from here(Navigation Drawer In Android Kotlin)

MainActivity.kt:

classMainActivity : AppCompatActivity(), View.OnClickListener {


    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        init()
    }

    privatefuninit() {
        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)

    }

    overridefunonBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }


    overridefunonClick(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!

Solution 2:

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()

Solution 3:

Is something like this? @NongthonbamTonthoi

classNavViewFragment : AppCompatActivity(), 
NavigationView.OnNavigationItemSelectedListener {

fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){
    supportFragmentManager.inTransaction { add(frameId, fragment) }
}



overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_nav_view_fragment)
    setSupportActionBar(toolbar)

    nav_view.setNavigationItemSelectedListener(this)
}

overridefunonBackPressed() {
    if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
        drawer_layout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

overridefunonCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.nav_view, menu)
    returntrue
}

overridefunonOptionsItemSelected(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 -> returntrueelse -> returnsuper.onOptionsItemSelected(item)
    }
}

overridefunonNavigationItemSelected(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)
    returntrue
    }
inlinefun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
    val fragmentTransaction = beginTransaction()
    fragmentTransaction.func()
    fragmentTransaction.commit()
}

}

Post a Comment for "Fragment In Kotlin"