Android toolbar overflow menu changes text color and fills menu to screen

there is

I am having to face the problem of changing the text color of a toolbar / action bar overflow menu item. I am doing too much googling and reading Stackoverflow answers but it cannot solve my problem.

enter image description here

currenlt I am getting black on the overflow menu item.

what i want 1. I want to change the color of the menu item black to WHITE. 2. I want to change the width of the overflow menu to fit the parent screen

my code:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColorSecondary">@color/secondary_text</item>
    <item name="android:divider">@color/divider</item>
    <item name="icon">@color/icons</item>
    <item name="actionOverflowMenuStyle">@style/AppTheme.Base.PopupMenu</item>
</style>

<style name="AppTheme.Base.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">@color/primary</item>
</style>

      

Please help me to solve this.

early

+3


source to share


2 answers


Finally I got the code for black and white to convert to white



public boolean onPrepareOptionsMenu(Menu menu) {
    //return super.onPrepareOptionsMenu(menu);
    try {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem mi = menu.getItem(i);
            mi.setIcon(R.drawable.ic_action_new);
            String title = mi.getTitle().toString();
            Spannable spannable = new SpannableString(title);
            spannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            mi.setTitle(spannable);
        }
    } catch (Exception ex) {

    }
    return true;
}

      

+1


source


A more flexible but less preferred way would be to use PopupWindow

example MainActivity.java package com.chiragjn.stackovefflowing;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    String TAG = "MainActivity.java";
    String popUpContents[];
    PopupWindow popupWindow;
    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        List<String> itemsList = new ArrayList<String>();
        itemsList.add("Option 1");
        itemsList.add("Option 2");
        itemsList.add("Option 3");
        itemsList.add("Option 4");
        popUpContents = new String[itemsList.size()];
        itemsList.toArray(popUpContents);
        popupWindow = popupWindow();
    }

    public PopupWindow popupWindow() {

        PopupWindow popupWindow = new PopupWindow(this);
        ListView listView = new ListView(this);
        listView.setAdapter(dogsAdapter(popUpContents));
        popupWindow.setFocusable(true);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(listView);
        return popupWindow;
    }

    private ArrayAdapter<String> dogsAdapter(String dogsArray[]) {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dogsArray) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                String item = getItem(position);
                String text = item;


                TextView listItem = new TextView(MainActivity.this);

                listItem.setText(text);
                listItem.setTextSize(22);
                listItem.setPadding(10, 10, 10, 10);
                listItem.setTextColor(Color.WHITE);
                switch(position)
                {
                    case 0:
                        listItem.setBackgroundColor(Color.BLUE);
                        break;
                    case 1:
                        listItem.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        listItem.setBackgroundColor(Color.YELLOW);
                        break;
                    case 3:
                        listItem.setBackgroundColor(Color.RED);
                        break;
                    default:
                    break;
                }
                return listItem;
            }
        };

        return adapter;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_options:
            View v = findViewById(R.id.action_options);
            popupWindow.showAsDropDown(v, 0, 0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

      



main.xml

<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="com.chiragjn.stackovefflowing.MainActivity" >

    <item android:id="@+id/action_options"
          android:icon="@android:drawable/ic_menu_more"
          android:title="Overflow Menu"
          app:showAsAction="always"  />
</menu>

      

Then you can inflate your favorite layout in the ListView and implement the ListView onItemClickListner ()

0


source







All Articles