2

scratching my head. if someone could help that would be great. I am switching from extends ActionBarActivity to extends AppCompatActivity with Toolbar and need help displaying items on toolbar.

I am using extends AppCompatActivity included android.support.v7.app.AppCompatActivity; then inside onCreate() have:

Toolbar jobListToolbar = (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(jobListToolbar);

then in xml file I have

 <android.support.v7.widget.Toolbar
        android:id="@+id/job_list_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:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

then in menu file I have items and everything seems to work, however not the way I want.

Toolbar/ActionBar is displayed on top of the screen, but no menu items showing on it. If I press phone original menu button then onCreateOptionsMenu(Menu menu) is initiated for the first time, and menu is showing. But it shows just above this phone button. But I want those buttons to show on Toolbar/ActionBar on top of the screen. I read somewhere that if phone has hardware menu button it will not show menu items in Toolbar/ActionBar, but I had it somehow done with extends ActionBarActivity, but there should be a way to do this with extends AppCompatActivity. Need to move to newer recommended way by google, but not sure how to show items on that Toolbar/ActionBar on the top of the screen.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Vaidas
  • 1,297
  • 1
  • 13
  • 14

2 Answers2

2

After a day of research on how to switch to AppCompat Toolbar finally found the solution. Two must do steps (No 2 being the one that nobody talks about):

I.) couple good resources on how to set up Toolbar can be found in this video Toolbar Tutorial and also in this StackOverflow: toolbar button

II.) MAKE SURE YOU PLACE THIS CODE AT THE BOTTOM OF onCreate():

toolbar= (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null) {
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

My problem was that I had some code in onCreate() that modified the toolbar buttons. That code needed to be run before toolbar was opened. If only first two lines of the code above included toolbar will show, but no menu will be inflated and no buttons. When I called getSupportActionBar().setDisplayShowHomeEnabled(true); thats when onCreateOptionsMenu(Menu menu) is called (if no true enabled it is not called), also make sure you have getMenuInflater().inflate(R.menu.job_list, menu); inside onCreateOptionsMenu(Menu menu).

If the code that calls for menu to open getSupportActionBar().setDisplayShowHomeEnabled(true); is not at the end of onCreate() you might be scratching your head why the menu is not showing. So make sure you execute your relevant onCreate() code before you call the menu to open.

Community
  • 1
  • 1
Vaidas
  • 1,297
  • 1
  • 13
  • 14
0

MyActivity:

extends  AppCompatActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarhome);
setSupportActionBar(toolbar);

activity_my.xml: If you want you can delete the last line 0 ep. They are the starting point of 0 to launching toolbar

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbarhome"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:popupTheme="@style/AppTheme.PopupOverlay"
      app:titleTextAppearance="@style/Toolbar.TitleText"
      app:layout_collapseMode="pin"
      android:background="@color/colorPrimary"
      android:contentInsetLeft="0dp"
      android:contentInsetStart="0dp"
      app:contentInsetLeft="0dp"
      app:contentInsetStart="0dp"
      android:contentInsetRight="0dp"
      android:contentInsetEnd="0dp"
      app:contentInsetRight="0dp"
      app:contentInsetEnd="0dp" >

        //designed the way you like

    </android.support.v7.widget.Toolbar>

manifest:

        <activity
            android:name=".MyActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

style:

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">    </style>

<style name="Toolbar.TitleText"    parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">italic</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
Ahmet Kocaman
  • 109
  • 11
  • Ahmet, thanks for the answer, but it's the same thing - Toolbar/ActionBar is showing on the top, but no menu items displayed on it. Menu items apear only when I press menu button on the Samsung phone itself. – Vaidas Aug 26 '16 at 15:07