0

I have 3 activities all use the same menu and 2 of the activities have a popupMenu or overflow menu. So on activity 2 I have the itemBackground set to RED and on the other activity 3 I would like to set the itemBacground to BLUE. I have defined the action in the menu as two different action. Android sees the background as one view. This post HERE talks about being able to have two TEXT sizes for the same popupMenu but not the itemBackground COLOR. I considered two menu.xml files but as long as android sees itemBackground as one distinct view that should not work. Not tested yet. Does anyone have a suggestion how to have two different background colors on each of my activities popupMenus?

not sure this helps but here is my res/values/styles code

<item name="android:itemBackground">@color/bgpopup</item>
Community
  • 1
  • 1
James_Duh
  • 1,321
  • 11
  • 31

1 Answers1

1

@James_Duh I spotted a post that is 6 years old that said this has been a problem I am not sure if Google has made a correction or not. Since you have a need to carry out some action on lets say Activity 3 You could add a TextView to the toolbar and tie a click listener to the text. Then you can style the text some color if the color is important. While this is not an exact answer to your question it is I hope a leaning alternative. Great question for a 7th grader Code below

This goes in the xml file associated with the Activity below the toolbar declation

        <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:textStyle="bold"
        android:textSize="22sp"/>

And this is the construct for the listener in the associated Activity

    private void addListenerOnTextView() {
    Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
    setSupportActionBar( tb );
    tb.findViewById( R.id.title ).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            System.out.println("Text Was Clicked");
        }
    } );
    getSupportActionBar().setTitle( null );
}

Ok this goes in the xml file

        <Button
        android:text="NEW"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnNew"
        android:layout_marginLeft="260dp"/>

and this is the listener in the Activity

    private void addListenerOnButtonNew() {
    Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
    setSupportActionBar( tb );
    tb.findViewById( R.id.btnNew ).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent( Activity2.this, Activity3.class );
            startActivity( intent );
        }
    } );
    getSupportActionBar().setTitle( null );
}

In my comment SO striped the spaces here is what it should look like

setTitle("      Page Three                   ");
Vector
  • 3,066
  • 5
  • 27
  • 54
  • Can I use a button in place of the text for some reason the text will not move to the right. I tried padding but it obliterates the title text – James_Duh Oct 14 '16 at 00:16
  • @James_Duh I do not know how you are styling your title text I used a sloppy concept to just take a look at the layout. I would not do this in production but for a look see it worked Let me post the code for a button here is the text workaround setTitle(" Page Three "); note the spaces – Vector Oct 14 '16 at 00:25
  • 1
    @James_Duh The issue with the TextView is perhaps because you do not see the menu items in the ToolBar when you are adding the TextView or Button. These are inflated and added at run time. Also the reason you can use these widgets in the ToolBar is because they are view items – Vector Oct 14 '16 at 01:11