androidkotlinandroid-actionbarandroid-toolbarandroid-tabbed-activity

Questions about ActionBar, Toolbar and menu icons


Assume a tabbed activity with android:theme="@style/AppTheme.NoActionBar"

Inside AppBarLayout there is the app title and some space to the right. Xml file is below.

How can I add menuitems to this empty space near the Title? (Question 1)

Changed style to android:theme="@style/AppTheme" (so that ActionBar appears again) and deleted title TextView. Menu item and icons appeared but the action bar did not look as smooth since there is shading below the action bar which makes it look separate from the tabs.

So, tried eliminating color difference by changing background with actionBar!!.setBackgroundDrawable(...) inside onCreate() of MainActivity, but got null pointer exception. Was actionBar not initialized? Where or how should I call this code? (Question 2)

Also tried enveloping the empty area by <Toolbar>...</Toolbar> inside xml file so that I can place the Title inside this Toolbar and define it as actionBar with the code below, but menu icons still did not appear (onCreateOptionsMenu is overridden inside MainActivity):

 val actionbar = toolBar // possible to skip findViewById
 setSupportActionBar(actionbar)
.
.
.
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.mainmenu, menu)
    return super.onCreateOptionsMenu(menu)
}

Here is the layout for tabbed activity.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:id="@+id/appBarMain">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:text="@string/app_name"
        android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:background="?attr/colorPrimary">

        <com.google.android.material.tabs.TabItem
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TEXT1" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TEXT2"/>

        <com.google.android.material.tabs.TabItem
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TEXT3"/>

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Bonus: If I place a ConstraintLayout inside AppBarLayout and set a clickListener onto imageviews of menu icons, would it be faster than an ActionBar's onOptionsItemSelected? If yes how faster? (Question 3)

P.S. Don't expect everyone to be experts. If everyone was experts what are you doing on that site? Google is not answer in all conditions. Keep in mind everyone does not have the luxury to spend hours searching or trying out things, too. Sometimes the only option left is to ask. If you feel burden to answer why don't you build your own community only for experts? (Question 3.6 as of March 2020)

EDIT: Using AndroidX Toolbar (<androidx.appcompat.widget.Toolbar>) in activity_main.xml worked for me. Looks like setSupportActionBar() requires AndroidX Toolbar and simple Toolbar can not be casted into it.


Solution

    1. Put toolbar inside appbar :
       <appbar>
         <toolbar>
         ....
         </toolbar>
       </appbar>
    

    then set toolbar as actionbar, then inflate the menu in onCreateOptionsMenu:

    setSupportActionBar(toolbar)

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.menu, menu)
            return true
    }
    
    1. it should be 'getSupportActionBar()' not 'getActionBar()'
    2. don't know if it's faster or not, but for action inside a toolbar, it's better to use 'menu' approach imo