Last active
September 17, 2023 02:32
-
-
Save LucasFebatis/ae9cb85a5646384bafbe0b90b4e76c53 to your computer and use it in GitHub Desktop.
ActionBar com o botão voltar usando o Material 3
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.example.myapplication | |
import android.os.Bundle | |
import android.view.MenuItem | |
import androidx.appcompat.app.AppCompatActivity | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Tornando o botão voltar da ActionBar visivel | |
supportActionBar?.setDisplayHomeAsUpEnabled(true) | |
} | |
// Adicionando ação para finalizar a activity quando apertar o botão voltar | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
android.R.id.home -> { | |
finish() | |
return true | |
} | |
} | |
return super.onOptionsItemSelected(item) | |
} | |
} |
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
<resources xmlns:tools="http://schemas.android.com/tools"> | |
<!-- Base application theme. --> | |
<style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight"> | |
<!-- Customize your light theme here. --> | |
<!-- <item name="colorPrimary">@color/my_light_primary</item> --> | |
</style> | |
<style name="Theme.MyApplication" parent="Base.Theme.MyApplication" /> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment