In questo articolo vedremo come creare OptionMenu diversi in due Activity diverse e nella seconda Activity il pulsante per tornare all'Activity principale.
Dopo aver creato una seconda Activity creiamo il menu specifico per questo layout come abbiamo visto in questo articolo modificando però il nome del file del menu ad esempio: menu_second.
Il risultato sarà un file che si chiama menu_second.xml al'interno della cartella menu
Ecco come si presenta il file menu_second.xml
Ecco come si presenta il file menu_main.xml
Una volta creato i menù occorre abilitarli all'interno del codice, quindi all'interno del file MainActivity.kt occorre inserire queste righe di codice dopo la funzione onCreate():
Con l'aggiunta di queste righe di codice è sufficiente per poter visualizzare il menù ma non per poter interagire con esso.
Per poter interagire con i vari elementi del menù occorre inserire queste righe di codice:
Ora passiamo al file SecondActivity.kt dove dobbiamo abilitare il tasto per tornare all'Activity principale inserendo queste righe di codice all'interno della funzione onCreate():
e abilitare anche l'OptionMenu in questo modo:
Ora la differenza sta nel gestire sia il pulsante per tornare indietro sia le varie voci dell'OptionMenu e bisogna procedere in questo modo inserendo questa funzione:
Ecco come si presenta il file MainActivity.kt
Ecco come si presenta il file SecondActivity.kt
Dopo aver creato una seconda Activity creiamo il menu specifico per questo layout come abbiamo visto in questo articolo modificando però il nome del file del menu ad esempio: menu_second.
Il risultato sarà un file che si chiama menu_second.xml al'interno della cartella menu
Ecco come si presenta il file menu_second.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"?> | |
<menu xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<item | |
android:id="@+id/action_el1" | |
android:orderInCategory="100" | |
android:title="@string/menu2_element1" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_el2" | |
android:orderInCategory="101" | |
android:title="@string/menu2_element2" | |
app:showAsAction="never" /> | |
</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"?> | |
<menu xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<item | |
android:id="@+id/action_el1" | |
android:orderInCategory="100" | |
android:title="@string/menu_element1" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_el2" | |
android:orderInCategory="101" | |
android:title="@string/menu_element2" | |
app:showAsAction="never" /> | |
<item | |
android:id="@+id/action_el3" | |
android:orderInCategory="102" | |
android:title="@string/menu_element3" | |
app:showAsAction="never" /> | |
</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
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inserisce il menu, questo aggiunge gli elementi del menu nell'ActionBar se presente | |
menuInflater.inflate(R.menu.menu_main, menu) | |
return true | |
} |
Per poter interagire con i vari elementi del menù occorre inserire queste righe di codice:
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
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
R.id.action_el1 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
} | |
R.id.action_el2 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
} | |
R.id.action_el3 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
} | |
else -> super.onOptionsItemSelected(item) | |
} | |
return true | |
} |
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
//Actionbar per tornare indietro | |
val actionbar = supportActionBar | |
//Impostazione titolo dell'ActionBar | |
actionbar!!.title = resources.getString(R.string.app_name) | |
//attivazione del tasto Indietro nell'ActionBar | |
actionbar.setDisplayHomeAsUpEnabled(true) |
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
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inserisce il menu, questo aggiunge gli elementi del menu nell'ActionBar se presente | |
menuInflater.inflate(R.menu.menu_second, menu) | |
return true | |
} |
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
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
android.R.id.home->{ | |
/*android.R.id.home fa riferimento al pulsante indietro | |
dichiarato con actionbar.setDisplayHomeAsUpEnabled(true)*/ | |
//questo permette di gestire il pulsante per tornare indietro | |
onBackPressed() | |
return true | |
} | |
R.id.action_el1 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
} | |
R.id.action_el2 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
} | |
else -> super.onOptionsItemSelected(item) | |
} | |
return true | |
} |
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.tutorialactionbar3 | |
import android.content.Intent //importazione della Classe: Intent | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.Menu //importazione della Classe: Menu | |
import android.view.MenuItem //importazione della Classe: MenuItem | |
import android.widget.Toast | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button.setOnClickListener { | |
//dichiarazione della variabile apriPaginaDue e associazione della classe Intent | |
//(this, nome della seconda Activity::class.java) | |
val apriPaginaDue = Intent(this, SecondActivity::class.java) | |
//avvio dell'Activity con passaggio della variabile apriPaginaDue | |
startActivity(apriPaginaDue) | |
} | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inserisce il menu, questo aggiunge gli elementi del menu nell'ActionBar se presente | |
menuInflater.inflate(R.menu.menu_main, menu) | |
return true | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
R.id.action_el1 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
Toast.makeText(this, "Hai premuto elemento 1", Toast.LENGTH_SHORT).show() | |
} | |
R.id.action_el2 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
Toast.makeText(this, "Hai premuto elemento 2", Toast.LENGTH_SHORT).show() | |
} | |
R.id.action_el3 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
Toast.makeText(this, "Hai premuto elemento 3", Toast.LENGTH_SHORT).show() | |
} | |
else -> super.onOptionsItemSelected(item) | |
} | |
return true | |
} | |
} |
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.tutorialactionbar3 | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.Menu //importazione della Classe: Menu | |
import android.view.MenuItem //importazione della Classe: MenuItem | |
import android.widget.Toast | |
class SecondActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_second) | |
//Actionbar per tornare indietro | |
val actionbar = supportActionBar | |
//Impostazione titolo dell'ActionBar | |
actionbar!!.title = resources.getString(R.string.app_name) | |
//attivazione del tasto Indietro nell'ActionBar | |
actionbar.setDisplayHomeAsUpEnabled(true) | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inserisce il menu, questo aggiunge gli elementi del menu nell'ActionBar se presente | |
menuInflater.inflate(R.menu.menu_second, menu) | |
return true | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
android.R.id.home->{ | |
onBackPressed() | |
return true | |
} | |
R.id.action_el1 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
Toast.makeText(this, "Hai premuto elemento 1 Activity 2", Toast.LENGTH_SHORT).show() | |
} | |
R.id.action_el2 ->{ | |
//inserire qui il codice che deve essere eseguito toccando questo elemento di menu | |
Toast.makeText(this, "Hai premuto elemento 2 Activity 2", Toast.LENGTH_SHORT).show() | |
} | |
else -> super.onOptionsItemSelected(item) | |
} | |
return true | |
} | |
} |
Download Project | ![]() |
Download file APK | ![]() |
- Ottieni link
- X
- Altre app
Etichette
ActionBar activity Android Studio
Etichette:
ActionBar
activity
Android Studio
Ubicazione:
Milano MI, Italia
- Ottieni link
- X
- Altre app
Commenti
Posta un commento