1

This question has been asked a number of times, but I cannot seem to hide the 3 dots icon.

There is no action_settings menu item in menu_main.xml, and yet the settings icon appears none the less.

I can't hide it in onPrepareOptionsMenu because there is no id for the icon

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item= menu.findItem(R.id.menu_settings);
    item.setVisible(false);
    super.onPrepareOptionsMenu(menu);
    return true;
}

My main activity lay out begins:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <LinearLayout

        ....

and layout toolbar_default is

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto"
    style="@style/ToolBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"/>

and menu_main.xml is

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_search"
        android:title="@string/menu_search"
        appcompat:actionViewClass="android.support.v7.widget.SearchView"
        appcompat:showAsAction="always"/>

</menu>

and ToolBarStyle

<style name="ToolBarStyle" parent="">
    <item name="popupTheme">@style/ThemeOverlay.AppCompat</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

<style name="CustomPrefTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:checkboxStyle">@style/AppTheme.Preference.Checkbox</item>
</style>
the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

2

Set the menu dynamically to your toolbar

 Toolbar yourToolBar = (Toolbar) findViewById(R.id.my_toobar);
 setSupportActionBar(yourToolBar);
 yourToolBar.inflateMenu(R.menu.search_menu);
 yourToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() 
 {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item.getItemId() == R.id.search) {
                // handle search logic here
            } 
            return true;
        }
    });

Instead of

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
} 

You should probably do

// This will hide the overflow menu icon
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
Rakhi Dhavale
  • 1,196
  • 12
  • 19
0

From here https://stackoverflow.com/a/12253892/4858147 try hiding R.id.action_settings

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item= menu.findItem(R.id.action_settings);
    item.setVisible(false);
    super.onPrepareOptionsMenu(menu);
    return true;
}
svkaka
  • 3,942
  • 2
  • 31
  • 55