D3vil_Mind
D3vil_Mind

Reputation: 339

Condition check before android menu inflation error

is there a possibility to inflate android menu on true of a condition, i hope so it is possible, if so here is my faulty code, what is my error in condition checking

and where should the condition checking code must be placed in order to stop the menu from inflating in case of conition check failure

    @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
         MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.bar, menu);
            return true;
     }

     @Override
        public boolean onOptionsItemSelected(MenuItem item){
         AlertDialog.Builder builder=new AlertDialog.Builder(this);

            builder.setTitle("Pick An Image")
                    .setMessage("Please select Image One or Image Two:")
                    .setCancelable(false)
                    .setPositiveButton("IMAGE 1", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface Dialog,int id){
                        //image.setImageResource(R.drawable.image1);
                        }
                    })
                    .setNeutralButton("IMAGE 3",new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface Dialog,int id){
                        //image.setImageResource(R.drawable.icon);
                        }
                    })
                    .setNegativeButton("Image 2", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog,int id){
                        //image.setImageResource(R.drawable.image2);
                        }
                    });
            Spinner spinner1=(Spinner)findViewById(R.id.spinner1);
            Spinner spinner2=(Spinner)findViewById(R.id.spinner2);
            int dim1=spinner1.getSelectedItemPosition();
            int dim2=spinner2.getSelectedItemPosition();

            if(dim1==dim2)
            {
          Toast.makeText(null, "your source and destination are same", Toast.LENGTH_SHORT).show();
            }
            else
            {
                switch(item.getItemId()){
            case R.id.buttonone:
                builder.show();
                return true;
            case R.id.buttontwo:
                builder.show();
                return true;
            case R.id.buttonthree:
                builder.show();
                return true;
            case R.id.buttonfour:
                builder.show();
                return true;
            case R.id.buttonfive:
                builder.show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
        return super.onOptionsItemSelected(item);


 }

in which line i do the condition check i can stop the menu from inflation during condition fail..

Upvotes: 0

Views: 917

Answers (1)

Sleepybear
Sleepybear

Reputation: 362

If I'm understanding you correctly, you will want to add your condition check in onCreateOptionsMenu() and return false if your condition is not met.

However, onCreateOptionsMenu() is only called the FIRST time a menu needs to be created, once one is created (true is returned) then it will not be called until it is invalidated for some reason (3.0+ does this on various occasions, but <3.0 does not) such as the activity closing. Any time the menu key is pressed after that the onPrepareOptionsMenu() (I think that's the method) is called before showing the menu.

Hope that is what you're looking for.

Upvotes: 2

Related Questions