2

In my application I want to set a custom menu for the toolbar. The icon that I defined in the menu_settings.xml is an arrow, but when I start the application, it's a three-point menu instead of the arrow (as seen in the Pictures). After hours of research I still can't figure out why it loads this icon instead of the arrow (yes I checked the drawable ;) ). Thank you for solutions and hints!

False Icon 1 False icon after click on three-point menu

SettingsActivity:

public class SettingsActivity extends PreferenceActivity {

private AppCompatDelegate mDelegate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();
    getDelegate().onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();
    boolean hasConfiguredSettings = intent.getBooleanExtra(HAS_CONFIGURED_SETTINGS, false);

    if (hasConfiguredSettings) {
        toolbar.setTitle("Wähle deine Linien!");


    } else {
        // Back button in Toolbar
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(R.id.content, new SettingsFragment())
            .commit();
}

private void setSupportActionBar(Toolbar toolbar) {
    getDelegate().setSupportActionBar(toolbar);
}

private AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
        mDelegate = AppCompatDelegate.create(this, null);
    }
    return mDelegate;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = getIntent();
    boolean hasConfiguredSettings = intent.getBooleanExtra("HAS_CONFIGURED_SETTINGS", false);
    if (hasConfiguredSettings) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_settings, menu);
        return true;
    }

    return false;
}
}

menu_settings.xml:

<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SettingsActivity">
<item
    android:id="@+id/action_checked"
    android:orderInCategory="101"
    android:title="@string/configuration_action_button"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_arrow_right"/>
</menu>

toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_height="wrap_content"
  android:layout_width="match_parent"
  android:background="@color/primary"
/>

The toolbar is included with:

<include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar" />
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
alexfi
  • 370
  • 2
  • 11
  • Yes. The true icon is: `ic_action_arrow_right` which looks like this: [http://www.icons4android.com/icon/1126](http://www.icons4android.com/icon/1126) – alexfi Aug 30 '15 at 16:22
  • The false icon looks like the icon in the picures, on the right side of the toolbar. It actually looks like this: [http://www.fileformat.info/info/unicode/char/205d/index.htm](http://www.fileformat.info/info/unicode/char/205d/index.htm) ... it's the so called Kebab menu ;) – alexfi Aug 30 '15 at 16:26
  • @FrankN.Stein He meant wrong/correct icon, but chose wrong words. – srka Aug 30 '15 at 16:29
  • I see. Possibly the right and the wrong icons are named inversely. Therefore, Android `seems` to swap them. – Phantômaxx Aug 30 '15 at 16:34
  • Sorry I m not a native Speaker. Changed the title of the question ;) No the names of the icons are correct. – alexfi Aug 30 '15 at 16:37
  • Try removing ´android:orderInCategory="101"´ – srka Aug 30 '15 at 16:51
  • I tried it, but it didn't had any impact on the icon. – alexfi Aug 30 '15 at 16:54
  • The problem is that menu item is shown in popup (like this: http://www.techotopia.com/images/8/87/Kindle_fire_overflow_action_toolbar.png ), so it can't have icon. Oveflow icon(three dots) can't be changed as far as I know. `app:showAsAction="always"` make it look as you want, but it isn't for some reason. Maybe try with `android:showAsAction="always"` (`android` instead of `app` prefix) – srka Aug 30 '15 at 17:03
  • 2
    Check out this post. http://stackoverflow.com/questions/29038861/how-to-change-toolbar-navigation-and-overflow-menu-icons-appcompat-v7 – K Neeraj Lal Aug 30 '15 at 17:05
  • @skra As I use AppCompat this doesn't work. – alexfi Aug 30 '15 at 17:51
  • @KNeerajLal yes this changes the button, thank you! Now I just need just to disable the popup with "Weiter" – alexfi Aug 30 '15 at 17:52

1 Answers1

1

You need to use AppCompatDelegate#getMenuInflater instead of Activity#getMenuInflater().

Replace the below line:

MenuInflater inflater = getMenuInflater();

with

MenuInflater inflater = getDelegate().getMenuInflater();
blizzard
  • 5,275
  • 2
  • 34
  • 48