Mudanças entre as edições de "Android Kotlin - Menus e Activities"
De Aulas
(8 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
+ | |||
Afluentes: [[Dispositivos Móveis]] | Afluentes: [[Dispositivos Móveis]] | ||
= Visual = | = Visual = | ||
+ | |||
+ | Nesse exemplo vamos criar um projeto com um '''Basic Activity''', mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities. | ||
+ | |||
+ | Nosso exemplo pronto deve parecer com as imagens abaixo. | ||
<center>[[Image:Android_studio_menus_activities.png|500px]]</center> | <center>[[Image:Android_studio_menus_activities.png|500px]]</center> | ||
Linha 9: | Linha 14: | ||
<syntaxhighlight lang=xml line> | <syntaxhighlight lang=xml line> | ||
<resources> | <resources> | ||
− | <string name="app_name"> | + | <string name="app_name">Menu and Activity Test</string> |
<string name="action_settings">Settings</string> | <string name="action_settings">Settings</string> | ||
<string name="action_other">Other</string> | <string name="action_other">Other</string> | ||
<string name="name">Name</string> | <string name="name">Name</string> | ||
<string name="age">Age</string> | <string name="age">Age</string> | ||
− | |||
<string name="button_execute">Execute</string> | <string name="button_execute">Execute</string> | ||
− | <string name="message">Your name is %1$s and | + | <string name="message">Your name is %1$s and your age are %2$s years old.</string> |
</resources> | </resources> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Linha 39: | Linha 43: | ||
== MainActivity.kt == | == MainActivity.kt == | ||
− | Nos programa kotlin, basta adicionar as ações dentro da opção no | + | Nos programa kotlin, basta adicionar as ações dentro da opção no <code>when</code>. |
<syntaxhighlight lang=kotlin n> | <syntaxhighlight lang=kotlin n> | ||
Linha 64: | Linha 68: | ||
<syntaxhighlight lang=xml n> | <syntaxhighlight lang=xml n> | ||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
− | < | + | <androidx.constraintlayout.widget.ConstraintLayout 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" | |
− | + | app:layout_behavior="@string/appbar_scrolling_view_behavior"> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<EditText | <EditText | ||
− | + | android:id="@+id/editTextName" | |
− | + | android:layout_width="0dp" | |
− | + | android:layout_height="wrap_content" | |
− | + | android:layout_marginTop="116dp" | |
− | + | android:ems="10" | |
− | + | android:hint="@string/name" | |
− | + | android:inputType="textPersonName" | |
− | + | app:layout_constraintEnd_toEndOf="parent" | |
− | + | app:layout_constraintHorizontal_bias="1.0" | |
+ | app:layout_constraintStart_toStartOf="parent" | ||
+ | app:layout_constraintTop_toTopOf="parent" /> | ||
+ | |||
<EditText | <EditText | ||
− | + | android:id="@+id/editTextAge" | |
− | + | android:layout_width="0dp" | |
− | + | android:layout_height="wrap_content" | |
− | + | android:ems="10" | |
− | + | android:hint="@string/age" | |
− | + | android:inputType="textPersonName" | |
− | + | app:layout_constraintEnd_toEndOf="parent" | |
− | + | app:layout_constraintStart_toStartOf="parent" | |
− | + | app:layout_constraintTop_toBottomOf="@+id/editTextName" /> | |
− | </android. | + | <TextView |
+ | android:id="@+id/textViewMessage" | ||
+ | android:layout_width="0dp" | ||
+ | android:layout_height="wrap_content" | ||
+ | android:layout_marginTop="32dp" | ||
+ | android:textColor="#734BB9" | ||
+ | android:textScaleX="1.0" | ||
+ | android:textSize="20sp" | ||
+ | android:textStyle="normal" | ||
+ | app:layout_constraintEnd_toEndOf="parent" | ||
+ | app:layout_constraintHorizontal_bias="0.0" | ||
+ | app:layout_constraintStart_toStartOf="parent" | ||
+ | app:layout_constraintTop_toBottomOf="@+id/editTextAge" /> | ||
+ | </androidx.constraintlayout.widget.ConstraintLayout> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == activity_other.xml == |
<syntaxhighlight lang=xml n> | <syntaxhighlight lang=xml n> | ||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
− | < | + | <androidx.constraintlayout.widget.ConstraintLayout 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=".OtherActivity"> | |
− | + | ||
+ | <TextView | ||
+ | android:id="@+id/textViewName" | ||
+ | android:layout_width="0dp" | ||
+ | android:layout_height="wrap_content" | ||
+ | android:layout_marginTop="36dp" | ||
+ | android:text="@string/name" | ||
+ | app:layout_constraintEnd_toEndOf="parent" | ||
+ | app:layout_constraintHorizontal_bias="0.0" | ||
+ | app:layout_constraintStart_toStartOf="parent" | ||
+ | app:layout_constraintTop_toTopOf="parent" /> | ||
<TextView | <TextView | ||
− | + | android:id="@+id/textViewAge" | |
− | + | android:layout_width="0dp" | |
− | + | android:layout_height="wrap_content" | |
− | + | android:layout_marginTop="24dp" | |
− | + | android:text="@string/age" | |
− | + | app:layout_constraintEnd_toEndOf="parent" | |
− | + | app:layout_constraintHorizontal_bias="0.0" | |
− | + | app:layout_constraintStart_toStartOf="parent" | |
− | + | app:layout_constraintTop_toBottomOf="@+id/textViewName" /> | |
+ | |||
<Button | <Button | ||
− | + | android:id="@+id/buttonExecute" | |
− | + | android:layout_width="wrap_content" | |
− | + | android:layout_height="wrap_content" | |
− | + | android:layout_marginTop="88dp" | |
− | + | android:text="@string/button_execute" | |
− | + | app:layout_constraintEnd_toEndOf="parent" | |
− | + | app:layout_constraintHorizontal_bias="0.498" | |
− | + | app:layout_constraintStart_toStartOf="parent" | |
− | + | app:layout_constraintTop_toBottomOf="@+id/textViewAge" /> | |
− | + | </androidx.constraintlayout.widget.ConstraintLayout> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | </ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Linha 164: | Linha 166: | ||
<syntaxhighlight lang=kotlin n> | <syntaxhighlight lang=kotlin n> | ||
− | package com. | + | package com.example.menuteste |
import android.app.Activity | import android.app.Activity | ||
Linha 171: | Linha 173: | ||
import com.google.android.material.snackbar.Snackbar | import com.google.android.material.snackbar.Snackbar | ||
import androidx.appcompat.app.AppCompatActivity | import androidx.appcompat.app.AppCompatActivity | ||
+ | import androidx.navigation.ui.AppBarConfiguration | ||
import android.view.Menu | import android.view.Menu | ||
import android.view.MenuItem | import android.view.MenuItem | ||
− | |||
import android.widget.EditText | import android.widget.EditText | ||
import android.widget.TextView | import android.widget.TextView | ||
+ | import androidx.activity.result.contract.ActivityResultContracts | ||
+ | import com.example.menuteste.databinding.ActivityMainBinding | ||
− | + | class MainActivity : AppCompatActivity() { | |
− | + | private lateinit var appBarConfiguration: AppBarConfiguration | |
+ | private lateinit var binding: ActivityMainBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||
− | setContentView( | + | binding = ActivityMainBinding.inflate(layoutInflater) |
− | setSupportActionBar(toolbar) | + | setContentView(binding.root) |
+ | setSupportActionBar(binding.toolbar) | ||
− | fab.setOnClickListener { view -> | + | binding.fab.setOnClickListener { view -> |
− | + | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
− | + | .setAction("Action", null).show() | |
− | |||
} | } | ||
} | } | ||
Linha 198: | Linha 203: | ||
} | } | ||
− | fun | + | private fun openOtherActivity() { |
val editTextName : EditText = findViewById(R.id.editTextName) | val editTextName : EditText = findViewById(R.id.editTextName) | ||
val editTextAge : EditText = findViewById(R.id.editTextAge) | val editTextAge : EditText = findViewById(R.id.editTextAge) | ||
Linha 205: | Linha 210: | ||
intent.putExtra("name", editTextName.text.toString()) | intent.putExtra("name", editTextName.text.toString()) | ||
intent.putExtra("age", editTextAge.text.toString()) | intent.putExtra("age", editTextAge.text.toString()) | ||
− | + | getResult.launch(intent) | |
} | } | ||
Linha 212: | Linha 217: | ||
R.id.action_settings -> true | R.id.action_settings -> true | ||
R.id.action_other -> { | R.id.action_other -> { | ||
− | + | openOtherActivity() | |
true | true | ||
} | } | ||
Linha 219: | Linha 224: | ||
} | } | ||
− | + | private val getResult = | |
− | + | registerForActivityResult( | |
− | if (resultCode == Activity.RESULT_OK) { | + | ActivityResultContracts.StartActivityForResult() |
− | + | ) { | |
− | + | if (it.resultCode == Activity.RESULT_OK) { | |
− | + | if (it.data != null) { | |
+ | val textViewMessage : TextView = findViewById(R.id.textViewMessage) | ||
+ | textViewMessage.text = it.data?.getStringExtra("message") | ||
+ | } | ||
} | } | ||
} | } | ||
− | |||
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Linha 235: | Linha 241: | ||
<syntaxhighlight lang=kotlin n> | <syntaxhighlight lang=kotlin n> | ||
− | package com. | + | package com.example.menuteste |
import android.content.Intent | import android.content.Intent | ||
Linha 244: | Linha 250: | ||
class OtherActivity : AppCompatActivity() { | class OtherActivity : AppCompatActivity() { | ||
− | |||
override fun onCreate(savedInstanceState: Bundle?) { | override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_other) | setContentView(R.layout.activity_other) | ||
− | val textViewName : TextView = findViewById(R.id.textViewName) | + | val textViewName: TextView = findViewById(R.id.textViewName) |
val textViewAge : TextView = findViewById(R.id.textViewAge) | val textViewAge : TextView = findViewById(R.id.textViewAge) | ||
Linha 258: | Linha 263: | ||
} | } | ||
− | val buttonExecute : Button = findViewById(R.id.buttonExecute) | + | val buttonExecute: Button = findViewById(R.id.buttonExecute) |
buttonExecute.setOnClickListener { | buttonExecute.setOnClickListener { | ||
− | val | + | val message = getString(R.string.message, textViewName.text, textViewAge.text) |
val data = Intent() | val data = Intent() | ||
− | data.putExtra(" | + | data.putExtra("message", message) |
setResult(RESULT_OK, data) | setResult(RESULT_OK, data) | ||
finish() | finish() |
Edição atual tal como às 20h48min de 3 de maio de 2023
Afluentes: Dispositivos Móveis
Visual
Nesse exemplo vamos criar um projeto com um Basic Activity, mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities.
Nosso exemplo pronto deve parecer com as imagens abaixo.
strings.xml
1<resources>
2 <string name="app_name">Menu and Activity Test</string>
3 <string name="action_settings">Settings</string>
4 <string name="action_other">Other</string>
5 <string name="name">Name</string>
6 <string name="age">Age</string>
7 <string name="button_execute">Execute</string>
8 <string name="message">Your name is %1$s and your age are %2$s years old.</string>
9</resources>
Menus
No menu_main.xml, adiciona-se os novos itens. A informação orderInCategory é a ordem que os itens do meno irão aparecer.
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="@+id/action_other"
android:title="@string/action_other"
android:orderInCategory="101"
app:showAsAction="never"/>
MainActivity.kt
Nos programa kotlin, basta adicionar as ações dentro da opção no when
.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val myText: TextView = findViewById(R.id.textViewHello)
return when (item.itemId) {
R.id.action_settings -> {
myText.text = "Settings option"
return true
}
R.id.action_other -> {
myText.text = "Other option"
return true
}
else -> super.onOptionsItemSelected(item)
}
}
Layouts
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<EditText
android:id="@+id/editTextName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/age"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextName" />
<TextView
android:id="@+id/textViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:textColor="#734BB9"
android:textScaleX="1.0"
android:textSize="20sp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextAge" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_other.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".OtherActivity">
<TextView
android:id="@+id/textViewName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="@string/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/age"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewName" />
<Button
android:id="@+id/buttonExecute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="@string/button_execute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAge" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin
MainActivity.kt
package com.example.menuteste
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.ui.AppBarConfiguration
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import com.example.menuteste.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
binding.fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
private fun openOtherActivity() {
val editTextName : EditText = findViewById(R.id.editTextName)
val editTextAge : EditText = findViewById(R.id.editTextAge)
val intent = Intent(this, OtherActivity::class.java)
intent.putExtra("name", editTextName.text.toString())
intent.putExtra("age", editTextAge.text.toString())
getResult.launch(intent)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
R.id.action_other -> {
openOtherActivity()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private val getResult =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {
if (it.data != null) {
val textViewMessage : TextView = findViewById(R.id.textViewMessage)
textViewMessage.text = it.data?.getStringExtra("message")
}
}
}
}
OtherActivity.kt
package com.example.menuteste
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val textViewName: TextView = findViewById(R.id.textViewName)
val textViewAge : TextView = findViewById(R.id.textViewAge)
val bundle = intent.extras
if (bundle != null) {
textViewName.text = bundle.getString("name")
textViewAge.text = bundle.getString("age")
}
val buttonExecute: Button = findViewById(R.id.buttonExecute)
buttonExecute.setOnClickListener {
val message = getString(R.string.message, textViewName.text, textViewAge.text)
val data = Intent()
data.putExtra("message", message)
setResult(RESULT_OK, data)
finish()
}
}
}