3

I want popup menu like this

enter image description here

that show it after click on button

XML

    <item

        android:state_pressed="true"
        android:id="@+id/fromFirstMonth"
        android:title="از ابتدای سال"
        android:drawable="@drawable/nav_item_background"/>
    <item
        android:state_pressed="true"
        android:id="@+id/currentMonth"
        android:title="این ماه"
        android:drawable="@color/blueMenu"/>
    <item
        xmlns:showAsAction="always"
        android:id="@+id/currentSession"
        android:title="این فصل"

        android:drawable="@color/white"/>
    <item
        xmlns:showAsAction="always"
        android:id="@+id/selection"
        android:title="تانتخابی"
        android:drawable="@color/blueMenu"/>

</menu>

Java

   hourglass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(Products.this, hourglass);
                //Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.hourglass_item, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(Products.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

                popup.show();//showing popup menu


            }
        });

I want set different background color for each item. i set android:drawable but this does not worked.

I can with this code can change text color but i does not know how can change background color item

 Menu menu=popup.getMenu();
MenuItem item = menu.getItem(0);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
        item.setTitle(s);

1 Answers1

1

If you want to use item tags and do this,as an alternative way you can tryBackgroundColorSpan and write the logic to reformat of the length of your strings.

s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);

out put:

enter image description here


else

If i do that i'll crate a Dialog without a Title

private Dialog fakeDialogUseInstedOfMenuItem;


fakeDialogUseInstedOfMenuItem = new Dialog(MainActivity.this);
fakeDialogUseInstedOfMenuItem.requestWindowFeature(Window.FEATURE_NO_TITLE); // no Title
fakeDialogUseInstedOfMenuItem.setContentView(R.layout.my_custom_view);

and set a fixed or scroll view for that as the requirement

my_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="200dp"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#789"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/first_lin"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:gravity="center">

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Option One"
                    android:textColor="#000"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFF"
                android:gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Option Two"
                    android:textColor="#000"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#FFF"></LinearLayout>
        </LinearLayout>
    </ScrollView>


</LinearLayout>

and access items like this,

LinearLayout linearLayoutOneInDialog = (LinearLayout) fakeDialogUseInstedOfMenuItem.findViewById(R.id.first_lin);

out put:

enter image description here

But these things are optional ways

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • this code only change background color of text and does not change whole width of item. –  Dec 29 '16 at 20:23
  • @chat kai yes! that's why i said it to keep it as an alternative and reformat the size of string to keep all items in the same size – Charuක Dec 29 '16 at 20:24
  • thank you,now how can reformat this size string that do same size item? –  Dec 29 '16 at 20:25
  • create this items using code, get the max size string,decide how much space you want to allocate,get the other strings sizes based on their size increase the number of space you need for a side compared to max one create new strings and load them – Charuක Dec 29 '16 at 20:31
  • Today i learn t a lesson trying to help here >http://stackoverflow.com/a/41376728/5188159 Algorithm is more than enough i told you the logic think and implement it.If you don't like it find something else :) – Charuක Dec 29 '16 at 20:35
  • ex: Largest string is 5 char.You add 10 to left 10 to right. all together 25. second string char is 3 . for make it 25 you need 22 so 11 per side.If second string is 2 in size 25-2 = 23 optional case add extra one to a side you prefer 12 + 2+ 11 , always keep the max size as it is – Charuක Dec 29 '16 at 20:41
  • @chat kai you won't use this solution.But if you didn't get a one you will use this thing.I have a suggestion if you can send a bounty i can share the code what do you say ;) – Charuක Dec 29 '16 at 20:46
  • how can check which item clicked? –  Jan 02 '17 at 21:23
  • in xml use onclick and use a method to accept view .. check which view got clicked .. i already posted how to onitialize items go get the view got clicked – Charuක Jan 02 '17 at 23:50