Reputation: 3535
I've read thousands of questions like this and seams even following the most basics tutorials I can't get it to work.
I've a toolbar with a menu, but the items icon doens't show up, neither at preview nor runtime:
The icons are drawable from svg and all of them show fine when I try to place at buttons
or imageviews
my menu is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.MenuAdsAppCompatActivity">
<item android:id="@+id/action_notification1"
android:icon="@drawable/ic_more"
android:title="action_notification">
<item android:id="@+id/action_share_us"
android:icon="@drawable/ic_share"
android:title="@string/share_us" />
<item android:id="@+id/action_rate_us"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/rate" />
<item android:id="@+id/action_about"
android:checked="false"
android:icon="@drawable/ic_about"
android:title="@string/about" />
</item>
</menu>
Another thing is at runtime the menu is covering the toolbar and i would like it to behave like the design preview where the menu is starting from the bottom of toolbar
===================UPDATE=============================
relevant activity methods:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar, menu);
return true;
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
super.setContentView(layoutResID);
ViewGroup v = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar, null);
this.helpView = (WebView) LayoutInflater.from(this).inflate(R.layout.html_dialog, null);
v.addView(toolbar, 0);
try {
ViewGroup.MarginLayoutParams layout = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
layout.setMargins(-v.getPaddingLeft(), -v.getPaddingTop(), -v.getPaddingRight(), v.getPaddingTop()+5);
toolbar.setLayoutParams(layout);
} catch (Exception e) {
e.printStackTrace();
}
this.setSupportActionBar(toolbar);
}
================================ UPDATE 2 ==============
With the suggestion of @Dhiraj the icons now are OK... but I still would like to make the menu NOT cover the toolbar
Upvotes: 4
Views: 4943
Reputation: 4879
Wrap your items inside <menu>
and add app:showAsAction="always"
to your parent item.
Updated menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".activities.MenuAdsAppCompatActivity">
<item android:id="@+id/action_notification1"
android:icon="@drawable/ic_more"
app:showAsAction="always"
android:title="action_notification">
<menu>
<item android:id="@+id/action_share_us"
android:icon="@drawable/ic_share"
android:title="@string/share_us" />
<item android:id="@+id/action_rate_us"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/rate" />
<item android:id="@+id/action_about"
android:checked="false"
android:icon="@drawable/ic_about"
android:title="@string/about" />
</menu>
</item>
</menu>
And to show menu below toolbar you need to provide actionOverflowMenuStyle
for your style.
For this please see : https://stackoverflow.com/a/34288434/3552066
Upvotes: 6