开发者

Launch submenu only if a particular conditions is true

开发者 https://www.devze.com 2023-03-15 16:01 出处:网络
Is there a way when a Menu item is clicked and before going to submenu开发者_如何学Go we have to check a condition, if it is valid we launch the submenu if not we cancel. Please Help me I\'m stuck wit

Is there a way when a Menu item is clicked and before going to submenu开发者_如何学Go we have to check a condition, if it is valid we launch the submenu if not we cancel. Please Help me I'm stuck with it ...

Thanks


okkk.. getting proble you should try like this..

public class Practice_TestActicvity extends Activity {
    SubMenu sub;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("this is first menu");
        menu.add("this is second menu");
        sub = menu.addSubMenu(0, 1, 0, "SubMenu");
        // sub.add(0,11,0,"SubMenu 1");
        return super.onCreateOptionsMenu(menu);
    }

and

     @Override
            public boolean onMenuItemSelected(int featureId, MenuItem item) {
                if (item.getTitle().toString().equals("SubMenu")) {
sub.clear();
                    //you have to put here condtion like
    //if(this==this){
                    Toast.makeText(this, "this is cliked", Toast.LENGTH_LONG).show();
                    sub.add(0, 11, 0, "SubMenu 1");
    //}
    //else{//execute this code
    //}

                }
                return super.onMenuItemSelected(featureId, item);
            }

now let me know which type of condition you wan to check?


Yes Ofcourse:

You can add/override onOptionsItemSelected(MenuItem item) method to get the results. See the example below:

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.logout:
       if(islogout){
         //code if islogout is setted to true and procceed further for submenu
         return true;
        }else{
         //code if islogout is setted to False and display Toast that could not procceed further.
         return false;
        }

    default:
        return super.onOptionsItemSelected(item);
    }
 }

I hope this helps.

Mark this answer Correct and with UpVote it it helps you!

Thanks
sHaH

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号