I want to add an icon that is used to log out, but when I include the following code the icon is displayed on the left, and I am interested in it being displayed on the right, this is my code:
// Icon
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cerrar)
//listener
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cerrar)
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
// Maneja la acción del botón en la ActionBar aquí
// Por ejemplo, para cerrar la actividad, puedes usar:
//finish()
cierre()
true
}
else -> super.onOptionsItemSelected(item)
}
}
As Pawan Harariya said, I needed to add this code:
menu_mail.xml
<?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"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/cerrar"
android:icon="@drawable/ic_cerrar"
app:showAsAction="always"
android:title="Cierre Sesión"/>
</menu>
and
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.cerrar -> {
cierre()
return true
}
else -> super.onOptionsItemSelected(item)
}
return super.onOptionsItemSelected(item)
}