Posts

[ KOTLIN ] NAVIGATION DRAWER || FRAGMENT || AND MODIFICATIION

Part 1 : How to Create Navigation Drawer
  • Create new project with Navigation Drawer Activity .
  •  Navigation bar automatically create




Part 2 : Modify , Delete or Create Items in Drawer
  •  Delate or Modify drawer items from file - res/menu/activity_main_drawer.xml
  •  Modify Header tab from  - layout/nav_header_main.xml
  •  Delete Header by deleting following line from activity_main.xml                                                         this line -    app:headerLayout="@layout/nav_header_main"






Part 3 : Fragment Tabs Impliment ( easy method )

  •   Step 1 - Replace class in MainActivity.kt with following class ( watch video tutorial)


class MainActivity: AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)


        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        displayScreen(-1)
    }

    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.main, menu)
        return true
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)

        }

    }

    fun  displayScreen (id: Int){
       val fragment = when (id){
   
   
            R.id.nav_camera -> {
                Main2Activity ()
            }



            else -> {
                Main2Activity()
            }
        }


supportFragmentManager.beginTransaction()
        .replace(R.id.relativelayout, fragment)
        .commit()

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        displayScreen(item.itemId)
        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }

}




  Step 2 - Add following code in file  layout/content_main.xml


  <RelativeLayout

        android:id="@+id/relativelayout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>


And change constaint layout to linear layout and All done.



  Step 3 - Create new empty activity
              and in kotlin file of that activity  replace code with following code


 Fragment() {


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val view: View = inflater!!.inflate(R.layout.fragment_blank_fragment2, container,
        false) 


     //   val tvv1 = view.findViewById<TextView>(R.id.tvv1) as TextView





return  view

   }
}
  • And All set.
Now we need to open this activity by tapping drawer sub-item
    for that just follow video tutorial.


Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.