Reputation: 993
I changed from a normal ActionBar to the new Toolbar. My problem is that my old menu doesn't work. When I click on the overflow icon nothing happens. I'm using appcompat v7 but I could not find any solution. Can someone help me?
Menufunctions:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.chatmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.onlineliste:
getOnlineList();
return true;
case R.id.settings:
showSettings();
return true;
case R.id.logout:
logout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Chatmenu:
<?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/onlineliste"
android:title="@string/userliste"
app:showAsAction="always">
</item>
<item
android:id="@+id/settings"
android:title="@string/settings"
app:showAsAction="never">
</item>
<item
android:id="@+id/logout"
android:title="@string/logout"
app:showAsAction="never">
</item>
</menu>
Theme
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#428bca</item>
<item name="colorPrimaryDark">#428bca</item>
<item name="android:windowNoTitle">true</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark</item>
<item name="windowActionBar">false</item>
</style>
Toolbar
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark" />
Upvotes: 2
Views: 2081
Reputation: 993
Oh my godness I got it. Thank you for all your help guys! The problem was that I had a RelativeLayout which was overlapping the toolbar but I did not see this directly. THe solution was to add
android:layout_below="@+id/toolbar"
to the relativ layout and now everything works fine.
Again thanks for all your help guys and sorry!
Upvotes: 6
Reputation:
You are not returning true from the onCreateOptionMenu().
The documentation says that
You must return true for the menu to be displayed; if you return false it will not be shown.
Insead of
return super.onCreateOptionsMenu(menu);
which always return false. Return true from onCreateOptionsMenu(menu);
Check out the documentation here. Source Link
Upvotes: 0
Reputation: 25194
Probably you need to set your Toolbar
as the activity ActionBar, using:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.your_toolbar);
setSupportActionBar(toolbar);
Upvotes: 0
Reputation: 18977
try this one:
<?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/onlineliste"
android:visible="true"
android:title="@string/userliste"
app:showAsAction="never">
</item>
<item
android:id="@+id/settings"
android:visible="true"
android:title="@string/settings"
app:showAsAction="never">
</item>
<item
android:id="@+id/logout"
android:visible="true"
android:title="@string/logout"
app:showAsAction="never">
</item>
also your theme must have a parent of Theme.AppCompat
and your Activity must extends from ActionBarActivity
Upvotes: 1