0

I have a popup menu that works. The user would press an image and a popup menu appears with 5 items. The problem is that I don't seem to be able to change the size of the text of the popup. The Java that calls the popup is as follows:

     public void TheCompanyMenu(View v) {
     PopupMenu mypopupmenu = new PopupMenu(this, v); 
     mypopupmenu.setOnMenuItemClickListener(this);  
     MenuInflater inflater = mypopupmenu.getMenuInflater(); 
     inflater.inflate(R.menu.popup, mypopupmenu.getMenu());
     mypopupmenu.show();
        }

        @Override 
        public boolean onMenuItemClick(MenuItem arg0) {
        switch (arg0.getItemId()) {  
        case R.id.option1:
           Intent intent1 = new Intent(this, MainActivity.class);
           startActivity(intent1);
        return true;
        case R.id.option2:
            Intent intent2 = new Intent(this, Item2.class);
            startActivity(intent2);
        return true;
        case R.id.option3:
            Intent intent3 = new Intent(this, Item3.class);
            startActivity(intent3);
        return true;    
        case R.id.option4:
            Intent intent4 = new Intent(this, Item4.class);
            startActivity(intent4);    
        return true;
        case R.id.option5:
        Intent intent5 = new Intent(this, Item5.class);
            startActivity(intent5);        
        return true;
        default:
        return super.onContextItemSelected(arg0);
        }
        }

The Menu xml called popup is as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/option1"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/HomeMenu"
     android:title="@string/HomeMenu" />

     <item android:id="@+id/option2"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option2"
     android:title="@string/option2menu" />

     <item android:id="@+id/option3"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option3"
     android:title="@string/option3menu" />

     <item android:id="@+id/option4"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option4"
     android:title="@string/option4menu" />

     <item android:id="@+id/option5"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option5"
     android:title="@string/option5Menu" />
     </menu>

I have tried changing the android:textSize to be dp but it has no effect.

The code in the style xml is:

 <style name="PopupItemStyle"> 
    <item name="android:background">#FFA0A0A0</item>
    <item name="android:gravity">center</item>
    <item name="android:padding">10dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">12sp</item>
 </style>

As I am fairly new to Java, I am keen to keep the java bit as it is (i.e. using popupmenu) as it is working (sort of), but need to change the text size. Thanks a lot!

user3079872
  • 191
  • 1
  • 5
  • 15
  • have you tried changing the size in style.xml? – vishalmullur Dec 28 '13 at 17:14
  • is that not what I am doing when I have: 12sp just above in the style xml? Should I have some other code? – user3079872 Dec 28 '13 at 17:16
  • Since there are two size options, popupmenu.xml and styles.xml, check editing in both. If not then try first in popupmenu.xml only and then in styles.xml only. Have you written the code yourself or are you referring from somewhere? – vishalmullur Dec 28 '13 at 17:18
  • I have done what you suggest, and the size is still the same, which by the way, it is far too big. I took the code from somewhere else, and just tweeked it for the menu items. Where I got it from had nothing about changing the text size. – user3079872 Dec 28 '13 at 17:25
  • I think it is not simple as setting a text size. have a look at this [link](http://stackoverflow.com/questions/20451357/changing-text-size-of-popup-menu-item) – vishalmullur Dec 28 '13 at 17:36
  • Thanks very much VyprNoch, should I use something else other than PopupMenu? What I want is that when the user clicks an image, a menu appears, with 5 selections, and they can select one of them. Each one will start a new activity. – user3079872 Dec 30 '13 at 19:18
  • Maybe you could try this [link](http://learnandroideasily.blogspot.in/2013/01/creating-context-menu-in-android.html). It opens a menu on long pressing the item. Here are two more links [this](http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/) and [this](http://mobile.tutsplus.com/tutorials/android/android-sdk-context-menus/) – vishalmullur Dec 31 '13 at 20:11
  • Thank you very much VyprNoch, I will investigate and see if this will be satisfactory for the user. Will let you know. Thanks again. – user3079872 Dec 31 '13 at 23:50

1 Answers1

0

actually,I also want do like this.but,I do it in another way.

private void showPopUp(View v) {
PopupMenu popup = new PopupMenu(DetailActivity.this, v);
LayoutInflater inflater = (LayoutInflater) DetailActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);

//link layout and popwindow
View myView  = inflater.inflate(R.layout.menu_popwindow, null);
PopupWindow myPopupWindow = new PopupWindow(myView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
myPopupWindow.setFocusable(true);//getfocus for editview        
//the follow help dismiss popup     myPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.linkme_menu));  
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setOnDismissListener(new OnDismissListener() { 
    @Override  
    public void onDismiss()  
    {  }   }});myPopupWindow.showAsDropDown(v);

i use it in actionbar,you can creat a new xml(menu_popwindow)by yourself.

paperhs
  • 194
  • 2
  • 6
  • Thanks paperhs. Does this code have to be used within actionbar? For this app, action bar has been disabled. – user3079872 Dec 28 '13 at 17:59
  • Of course not!you should use it in other cases.you can try it. – paperhs Dec 29 '13 at 13:48
  • Thank you paperhs. I am trying this now. What does your "DetailAcitivity" do? – user3079872 Dec 29 '13 at 22:57
  • Hi Paperhs, I have your code, but on clicking the button nothing happens. I don't have space to put the code here, but the beginning of it is: public void TheCompanyMenu(View v) { PopupMenu mypopupmenu = new PopupMenu(this, v); mypopupmenu.setOnMenuItemClickListener(this); LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.GRAY); – user3079872 Dec 30 '13 at 11:40
  • the detailactivity is the current activity.the view v is the button view,do you creat a xml named menu_popwindow.I am sorry it didn't help you. – paperhs Dec 31 '13 at 10:42
  • Hi Paperhs. the menu XML is called popup.xml. The problem was that nothing happened when the button was clicked. Strange. Will try again tomorrow. – user3079872 Jan 01 '14 at 23:45