0% found this document useful (0 votes)
63 views5 pages

Navigation Drawer en Kotlin

The document contains code for developing a mobile application in Kotlin. It shows how to create an activity with a navigation drawer and toolbar. The activity implements navigation item selection listeners to handle clicks on different items in the drawer. Toast messages are used to indicate which item was clicked. The drawer is closed after an item is selected.

Uploaded by

Zeus manluli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views5 pages

Navigation Drawer en Kotlin

The document contains code for developing a mobile application in Kotlin. It shows how to create an activity with a navigation drawer and toolbar. The activity implements navigation item selection listeners to handle clicks on different items in the drawer. Toast messages are used to indicate which item was clicked. The drawer is closed after an item is selected.

Uploaded by

Zeus manluli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Desarrollo de Aplicaciones Móviles

Leonardo Miguel Moreno Villalba

Zamora Pérez Miriam Judith

15701
• Código
import android.content.res.Configuration
import android.os.Bundle
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem

class MainActivity : AppCompatActivity() {

private lateinit var drawer: DrawerLayout


private lateinit var toggle: ActionBarDrawerToggle

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val toolbar: Toolbar = findViewById(R.id.toolbar_main)


setSupportActionBar(toolbar)

drawer = findViewById(R.id.drawer_layout)

toggle = ActionBarDrawerToggle(this, drawer, toolbar,


R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer.addDrawerListener(toggle)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
}

// ...
}

class MainActivity : AppCompatActivity() {

// ...
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
toggle.syncState()
}

override fun onConfigurationChanged(newConfig: Configuration?) {


super.onConfigurationChanged(newConfig)
toggle.onConfigurationChanged(newConfig)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {


if (toggle.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
}

class MainActivity : AppCompatActivity(),


NavigationView.OnNavigationItemSelectedListener {
// ...
}

class MainActivity : AppCompatActivity(),


NavigationView.OnNavigationItemSelectedListener {

// ...

override fun onNavigationItemSelected(item: MenuItem): Boolean {

when (item.itemId) {
R.id.nav_item_one -> Toast.makeText(this, "Clicked item one",
Toast.LENGTH_SHORT).show()
R.id.nav_item_two -> Toast.makeText(this, "Clicked item two",
Toast.LENGTH_SHORT).show()
R.id.nav_item_three -> Toast.makeText(this, "Clicked item three",
Toast.LENGTH_SHORT).show()
R.id.nav_item_four -> Toast.makeText(this, "Clicked item four",
Toast.LENGTH_SHORT).show()
}
return true
}
}

class MainActivity : AppCompatActivity(),


NavigationView.OnNavigationItemSelectedListener {
// ...
override fun onCreate(savedInstanceState: Bundle?) {
// ...
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener(this)
// ...
}
// ...

override fun onNavigationItemSelected(item: MenuItem): Boolean {

when (item.itemId) {
R.id.nav_item_one -> Toast.makeText(this, "Clicked item one",
Toast.LENGTH_SHORT).show()
R.id.nav_item_two -> Toast.makeText(this, "Clicked item two",
Toast.LENGTH_SHORT).show()
R.id.nav_item_three -> Toast.makeText(this, "Clicked item three",
Toast.LENGTH_SHORT).show()
R.id.nav_item_four -> Toast.makeText(this, "Clicked item four",
Toast.LENGTH_SHORT).show()
}
drawer.closeDrawer(GravityCompat.START)
return true
}

override fun onBackPressed() {


if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
• Resultado

You might also like