Abbiamo visto in questo articolo come visualizzare il VersionName e VersionCode all'interno di un messaggio "Toast".
In questo articolo vedremo come visualizzare il Version Name e il Version Code all'interno di una "TextView" cliccando una voce dal menù (tre puntini in alto a destra).
Aggiungere il campo TextView dove visualizzare il nome versione e il codice versione nel file content_main.xml
Ora il codice da copiare all'interno della funzione onOptionsItemSelected del file MainActivity.kt è il seguente
Il risultato finale del file MainActivity.kt
Aggiungere la dichiarazione della variabile alla riga 16
e aggiungere la relazione variabile - oggetto alla riga 23
In questo articolo vedremo come visualizzare il Version Name e il Version Code all'interno di una "TextView" cliccando una voce dal menù (tre puntini in alto a destra).
Aggiungere il campo TextView dove visualizzare il nome versione e il codice versione nel file content_main.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"?> | |
<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/activity_main" | |
tools:context=".MainActivity"> | |
<!--Aggiunta del campo TextView--> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="TextView" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
android:id="@+id/textVersion" | |
android:textSize="22sp"/> | |
</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
val context = applicationContext | |
val packageManager = context.packageManager | |
val packageName = context.packageName | |
/*recupero del nome versione*/ | |
var versionName = "non disponibile" | |
try { | |
versionName = packageManager.getPackageInfo(packageName, 0).versionName | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
/*recupero del codice versione*/ | |
var versionCode: Int? = 0 | |
try { | |
versionCode = packageManager.getPackageInfo(packageName, 0).versionCode | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
/*visualizzazione in una TextView | |
quindi occorre dichiarare la variabile myText all'interno della classe MainActivity*/ | |
myText!!.setText("VersionName = $versionName \n VersionCode = $versionCode") | |
//il carattere \n inserito prima di VersionCode serve per mandare a capo il testo all'interno | |
//di una TextView |
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.tutorialversionname2 | |
import android.content.pm.PackageManager | |
import android.os.Bundle | |
import android.support.design.widget.Snackbar //importazione della Classe: SnackBar | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Menu | |
import android.view.MenuItem | |
import android.widget.TextView //importazione della Classe: TextView | |
import kotlinx.android.synthetic.main.activity_main.* | |
import kotlinx.android.synthetic.main.content_main.* | |
class MainActivity : AppCompatActivity() { | |
var myText: TextView? = null //dichiarazione della variabile TextView dove verrà visualizzato il VersionName e VersionCode | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setSupportActionBar(toolbar) | |
myText = findViewById<TextView>(R.id.textVersion) | |
fab.setOnClickListener { view -> | |
Snackbar.make(view, "No action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show() | |
} | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
// Inflate the menu; questo aggiunge elementi alla barra delle azioni se è presente. | |
menuInflater.inflate(R.menu.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 / Su, | |
//purché si specifichi un'attività principale in AndroidManifest.xml. | |
when (item.itemId) { | |
R.id.action_info -> | |
{ | |
val context = applicationContext | |
val packageManager = context.packageManager | |
val packageName = context.packageName | |
/*recupero del nome versione*/ | |
var versionName = "non disponibile" | |
try { | |
versionName = packageManager.getPackageInfo(packageName, 0).versionName | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
/*recupero del codice versione*/ | |
var versionCode: Int? = 0 | |
try { | |
versionCode = packageManager.getPackageInfo(packageName, 0).versionCode | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
/*visualizzazione in una TextView | |
quindi occorre dichiarare la variabile myText all'interno della classe MainActivity*/ | |
myText!!.setText("VersionName = $versionName \n VersionCode = $versionCode") | |
//il carattere \n inserito prima di VersionCode serve per mandare a capo il testo all'interno | |
//di una TextView | |
} | |
} | |
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
var myText: TextView? = null |
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
myText = findViewById<TextView>(R.id.textVersion) |
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento