Navigation Drawner Activity
Navigation Drawner Activity è un tipo di progetto in cui è presente un menù laterale a sinistra (tre linee) e il menù a discesa in alto a destra (tre puntini)
AndroidManifest.xml
activity_main_drawer.xml
content_main.xml
activity_main.xml
app_bar_main.xml
MainActivity.kt
Navigation Drawner Activity è un tipo di progetto in cui è presente un menù laterale a sinistra (tre linee) e il menù a discesa in alto a destra (tre puntini)
AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.dm.tutorialnavigationdraweractivity"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme.NoActionBar"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<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/nav_home" | |
android:icon="@drawable/ic_menu_camera" | |
android:title="@string/menu_home"/> | |
<item | |
android:id="@+id/nav_gallery" | |
android:icon="@drawable/ic_menu_gallery" | |
android:title="@string/menu_gallery"/> | |
<item | |
android:id="@+id/nav_slideshow" | |
android:icon="@drawable/ic_menu_slideshow" | |
android:title="@string/menu_slideshow"/> | |
<item | |
android:id="@+id/nav_tools" | |
android:icon="@drawable/ic_menu_manage" | |
android:title="@string/menu_tools"/> | |
</group> | |
<item android:title="Communicate"> | |
<menu> | |
<item | |
android:id="@+id/nav_share" | |
android:icon="@drawable/ic_menu_share" | |
android:title="@string/menu_share"/> | |
<item | |
android:id="@+id/nav_send" | |
android:icon="@drawable/ic_menu_send" | |
android:title="@string/menu_send"/> | |
</menu> | |
</item> | |
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_behavior="@string/appbar_scrolling_view_behavior" | |
tools:showIn="@layout/app_bar_main" | |
tools:context=".MainActivity"> | |
<!--Aggiunta di un TextView (casella di testo)--> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Tutorial navigation drawer activity" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
android:textSize="28sp"/> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.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/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true" | |
tools:openDrawer="start"> | |
<include | |
layout="@layout/app_bar_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
<android.support.design.widget.NavigationView | |
android:id="@+id/nav_view" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:fitsSystemWindows="true" | |
app:headerLayout="@layout/nav_header_main" | |
app:menu="@menu/activity_main_drawer"/> | |
</android.support.v4.widget.DrawerLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.CoordinatorLayout | |
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:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<android.support.design.widget.AppBarLayout | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
android:theme="@style/AppTheme.AppBarOverlay"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:background="?attr/colorPrimary" | |
app:popupTheme="@style/AppTheme.PopupOverlay"/> | |
</android.support.design.widget.AppBarLayout> | |
<include layout="@layout/content_main"/> | |
<android.support.design.widget.FloatingActionButton | |
android:id="@+id/fab" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="bottom|end" | |
android:layout_margin="@dimen/fab_margin" | |
app:srcCompat="@android:drawable/ic_dialog_email"/> | |
</android.support.design.widget.CoordinatorLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dm.tutorialnavigationdraweractivity | |
import android.os.Bundle | |
import android.support.design.widget.FloatingActionButton | |
import android.support.design.widget.Snackbar | |
import android.support.v4.view.GravityCompat | |
import android.support.v7.app.ActionBarDrawerToggle | |
import android.view.MenuItem | |
import android.support.v4.widget.DrawerLayout | |
import android.support.design.widget.NavigationView | |
import android.support.v7.app.AppCompatActivity | |
import android.support.v7.widget.Toolbar | |
import android.view.Menu | |
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val toolbar: Toolbar = findViewById(R.id.toolbar) | |
setSupportActionBar(toolbar) | |
val fab: FloatingActionButton = findViewById(R.id.fab) | |
fab.setOnClickListener { view -> | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show() | |
} | |
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) | |
val navView: NavigationView = findViewById(R.id.nav_view) | |
val toggle = ActionBarDrawerToggle( | |
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close | |
) | |
drawerLayout.addDrawerListener(toggle) | |
toggle.syncState() | |
navView.setNavigationItemSelectedListener(this) | |
} | |
override fun onBackPressed() { | |
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) | |
if (drawerLayout.isDrawerOpen(GravityCompat.START)) { | |
drawerLayout.closeDrawer(GravityCompat.START) | |
} else { | |
super.onBackPressed() | |
} | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inflate the menu; questo aggiunge elementi alla barra delle azioni se è presente. | |
menuInflater.inflate(R.menu.main, menu) | |
return true | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
// Gestisci i clic degli elementi della barra delle azioni qui. La barra | |
// delle azioni gestirà automaticamente i clic sul pulsante Home/Up, | |
// purché si specifichi un'attività principale in AndroidManifest.xml. | |
return when (item.itemId) { | |
R.id.action_settings -> true | |
else -> super.onOptionsItemSelected(item) | |
} | |
} | |
override fun onNavigationItemSelected(item: MenuItem): Boolean { | |
// Gestisci i clic degli elementi della vista di navigazione qui. | |
when (item.itemId) { | |
R.id.nav_home -> { | |
// Gestisci l'azione della videocamera | |
} | |
R.id.nav_gallery -> { | |
// Gestisci l'azione della galleria | |
} | |
R.id.nav_slideshow -> { | |
} | |
R.id.nav_tools -> { | |
} | |
R.id.nav_share -> { | |
} | |
R.id.nav_send -> { | |
} | |
} | |
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout) | |
drawerLayout.closeDrawer(GravityCompat.START) | |
return true | |
} | |
} |
Download Project | ![]() |
Download file APK | ![]() |
Visualizza su | ![]() |
Commenti
Posta un commento