javaandroidoverflow-menu

Change the color of OverFlow Menu


This is my Starting Activity. It is working fine programmatically.

The only problem which I'm facing is that the OverFlow-Menu comes is dark theme when called by the Hardware Menu key.

I've provided all the essential lines of code in this sample.

public class StartingActivity extends AppCompatActivity implements LoginActivity_Tab.loginActivityTab_interface {

private static final String LOGIN_URL = "http://rollhack.96.lt/login.php";
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting);

    includeWidgets();
}

private void includeWidgets() {

    /////   Tab Layout   /////

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(mViewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    /////   ToolBar     /////

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle("Note Pad");
}

/////   This method is called for Exiting the App   /////
public void exitApp() {
    final AlertDialog.Builder exit = new AlertDialog.Builder(StartingActivity.this);
    exit.setTitle("Exit Application")
            .setMessage("Are you sure you want to Exit the application?")
            .setCancelable(true)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    StartingActivity.this.finishAffinity();
                }
            })
            .setNegativeButton(android.R.string.no, null);
    exit.create().show();
}

/////   This method is called to assign the Key Events  /////
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            exitApp();
            return true;
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_starting_activity, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_settings:
            return Boolean.parseBoolean(null);
        case (R.id.menu_about):
            return Boolean.parseBoolean(null);
        case (R.id.menu_exit):
            exitApp();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

This is how it looks when it is called by the overflow menu icon of the ActionBar

This is how my App OverFlow-Menu looks when Hardware Menu key is pressed


Solution

  • Overflow menu by Icon

    You can change the color of the overflow menu background by adding a new style in style.xml.

     <style name="OverflowMenu"
       parent="@android:style/Theme.Holo">
     <item name="android:popupMenuStyle">@style/MyOverflowMenu</item>
     <item name="android:itemTextAppearance">@style/TextAppearance</item>
    </style>
    <style name="MyOverflowMenu" 
       parent="@android:style/Widget.Holo.ListPopupWindow">
    <item name="android:popupBackground">@color/your_color</item> 
    </style>
    <style name="TextAppearance">
    <item name="android:textColor">@color/your_color</item>
    </style>
    

    Overflow Menu by Hardware Key

    To change the color of Overflow menu called by Hardware key, add another style.

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="actionBarPopupTheme">@style/OverflowMenuHardware</item>
      </style>
    
      <style name="OverflowMenuHardware" parent="ThemeOverlay.AppCompat.Dark">
      <item name="android:textColorSecondary">@color/your_color</item>
      <item name="android:colorBackground">@color/your_color</item>
      </style>
    

    Implement either one of these to change the color of Overflow menu of either the one called by icon(former code) or by hardware key(latter code).