Reputation: 23
enter image description here(here is a pic of my toolbar with the menu items)
I have a toolbar with 4 menu items but I cannot figure out a way to add divderlines between each item. Heres is my .xml file for the toolbar
EDITED
This is what i have so far and its still not creating the separators
<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="com.mycompany.materialtest.MainActivity">
<group android:id="@+id/homegroup"
android:checkableBehavior="single">
<item
android:id="@+id/home"
android:title="home"
android:orderInCategory="1"
android:icon="@drawable/homeclicked"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/searchgroup"
android:checkableBehavior="single">
<item
android:id="@+id/search"
android:title="search"
android:orderInCategory="2"
android:icon="@drawable/search"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/chatgroup"
android:checkableBehavior="single">
<item
android:id="@+id/chat"
android:title="chat"
android:orderInCategory="3"
android:icon="@drawable/chat"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/profilegroup"
android:checkableBehavior="single">
<item
android:id="@+id/profile"
android:title="profile"
android:orderInCategory="4"
android:icon="@drawable/user"
app:showAsAction="always">
</item>
</group>
</menu>
I have also seperated the menu items on my mainactivity.java. Here is the code if that helps in anyway.
public void setupEvenlyDistributedToolbar(Toolbar toolbar) {
// Use Display metrics to get Screen Dimensions
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
toolbar.inflateMenu(R.menu.menu_bottombar);
// Add 10 spacing on either side of the toolbar
toolbar.setContentInsetsAbsolute(10, 10);
// Get the ChildCount of your Toolbar, this should only be 1
int childCount = toolbar.getChildCount();
// Get the Screen Width in pixels
int screenWidth = metrics.widthPixels;
// Create the Toolbar Params based on the screenWidth
Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);
// Loop through the child Items
for (int i = 0; i < childCount; i++) {
// Get the item at the current index
View childView = toolbar.getChildAt(i);
// If its a ViewGroup
if (childView instanceof ViewGroup) {
// Set its layout params
childView.setLayoutParams(toolbarParams);
// Get the child count of this view group, and compute the item widths based on this count & screen size
int innerChildCount = ((ViewGroup) childView).getChildCount();
int itemWidth = (screenWidth / innerChildCount);
// Create layout params for the ActionMenuView
ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, Toolbar.LayoutParams.WRAP_CONTENT);
// Loop through the children
for (int j = 0; j < innerChildCount; j++) {
View grandChild = ((ViewGroup) childView).getChildAt(j);
if (grandChild instanceof ActionMenuItemView) {
// set the layout parameters on each View
grandChild.setLayoutParams(params);
}
}
}
}
}
}`
Any help would be great thanks!
Upvotes: 1
Views: 387
Reputation: 161
Try this in XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/home"
android:checkableBehavior="single">
<item
android:id="@+id/homegroup"
android:title="@string/next"
android:orderInCategory="1"
android:icon="@drawable/homeclicked"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/searchgroup"
android:checkableBehavior="single">
<item
android:id="@+id/search"
android:title="@string/next"
android:orderInCategory="2"
android:icon="@drawable/search"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/chatgroup"
android:checkableBehavior="single">
<item
android:id="@+id/chat"
android:title="@string/next"
android:orderInCategory="3"
android:icon="@drawable/chat"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/profilegroup"
android:checkableBehavior="single">
<item
android:id="@+id/profile"
android:title="@string/next"
android:orderInCategory="4"
android:icon="@drawable/profile"
app:showAsAction="always">
</item>
</group>
</menu>
Upvotes: 1