Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Using PopupMenu in API level 9

I like to add a PopupMenu to my application. The problem is that it should work on Android 2.3, too. I found some posts where people suggest to use AlertDialog as an alternative, but I prefer a PopupMenu ;)

I think it should work in this API-level, too because I've seen it in several apps (my phone has 2.3.5 and it work fine).

Is there any possibility to make this work?

like image 604
snix Avatar asked Sep 05 '12 12:09

snix


2 Answers

  • You must import support v7 in your application same as follow: Adding libraries with resources

  • import android.support.v7.widget.PopupMenu;

  • Compile your code with that and then your popup menu is compatible for android 2.2 and above.

like image 100
user3266062 Avatar answered Sep 27 '22 18:09

user3266062


PopupMenu is possible you can try this in a method for sending an email and you can inflate your xml according to your own needs:

    LayoutInflater inflater = (LayoutInflater)EEActionListDetail.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Display display = getWindowManager().getDefaultDisplay();

    int width = display.getWidth()/2;
    int height = display.getHeight()/2;

    View pop = inflater.inflate(R.layout.popupemail,null,false);
    pop.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);
    height = pop.getMeasuredHeight();
    width = pop.getMeasuredWidth()+200;
    pu = new PopupWindow(pop,width,height,true);
    pu.showAtLocation(findViewById(R.id.ll3),Gravity.CENTER,1,1);

    Button brnSend = (Button)pu.getContentView().findViewById(R.id.btnSend);
    Button close = (Button)pu.getContentView().findViewById(R.id.close);

    Subject = (EditText)pu.getContentView().findViewById(R.id.subject);
    Message = (EditText)pu.getContentView().findViewById(R.id.message);

    close.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            pu.dismiss();

        }
    });
    brnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for(int j=0;j<EmailArray.size();j++){
                String EmailSent = EmailArray.get(j);
                SendEmailALL(EmailSent);
            }
        }
    });
like image 23
raghav chopra Avatar answered Sep 27 '22 18:09

raghav chopra