In questo articolo vedremo come modificare il titolo dell'ActionBar.
Per fare questo è necessario inserire le seguenti righe di codice
Ecco come si presenta il file activity_main.xml
Ecco come si presenta il file MainActivity.kt
Ecco un video dimostrativo
Per fare questo è necessario inserire le seguenti 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
val actionBar = supportActionBar | |
actionBar!!.title = message |
Ecco come si presenta il file activity_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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".MainActivity" > | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="text|textCapSentences" | |
android:text="Nuovo titolo ActionBar" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Imposta titolo" /> | |
<Button | |
android:id="@+id/button2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Ripristina titolo" /> | |
</LinearLayout> |
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.tutorialrinominatitolo | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.Button | |
import android.widget.EditText | |
class MainActivity : AppCompatActivity() { | |
private var edit: EditText? = null | |
private var bnSet: Button? = null | |
private var bnReset: Button? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
edit = findViewById(R.id.editText) | |
bnSet = findViewById(R.id.button) | |
bnReset = findViewById(R.id.button2) | |
//impostazione della variabile actionBar con supportActionBar | |
val actionBar = supportActionBar | |
//pulsante che recupera il testo inserito nella EditText e lo | |
//imposta come Titolo dell'ActionBar | |
bnSet!!.setOnClickListener { | |
val message: String = edit!!.text.toString() | |
//impostazione del Titolo dell'ActionBar | |
actionBar!!.title = message | |
} | |
//pulsante che ripristina il Titolo originale dell'ActionBar | |
bnReset!!.setOnClickListener { | |
//ripristino del Titolo predefinito recuperato dal file string.xml | |
actionBar!!.title = resources.getString(R.string.app_name) | |
} | |
} | |
} |
Ecco un video dimostrativo
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento