user4622062
user4622062

Reputation:

change background color single item menu

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);

Upvotes: 3

Views: 1857

Answers (1)

Charuka Silva
Charuka Silva

Reputation: 13153

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

Upvotes: 1

Related Questions