Skip to content

Instantly share code, notes, and snippets.

@dandvl
Last active April 11, 2022 03:42
Show Gist options
  • Save dandvl/de67fd7383b83b461da1dd882542e5d3 to your computer and use it in GitHub Desktop.
Save dandvl/de67fd7383b83b461da1dd882542e5d3 to your computer and use it in GitHub Desktop.
Setting up a Toolbar
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
dependencies {
...
implementation 'androidx.appcompat:appcompat:1.4.1'
}

In the begging android had the Action Bar as an App bar (that's why remove on the manifest) but more features were added to the Toolbar. available now in this library. 'androidx.appcompat:appcompat:1.4.1'

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
// Note that the Toolbar defined in the layout has the id "my_toolbar"
setSupportActionBar(findViewById(R.id.my_toolbar))
}
}
supportActionBar?.title = "Title"
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_actiobar, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.action_favorite -> {
Toast.makeText(this@MainActivity, "ayayay", Toast.LENGTH_LONG).show()
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:allowBackup="true"
<?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">
<item
android:id="@+id/action_favorite"
android:icon="@drawable/ic_launcher_background"
android:title="item"
app:showAsAction="ifRoom"/>
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment