Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:15:49+00:00 2026-05-27T23:15:49+00:00

Android code for getting Options Menu programmatically in android after displaying the Activity?? Is

  • 0

Android code for getting Options Menu programmatically in android after displaying the Activity??

Is this possible?
Thanks in advance!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:15:49+00:00Added an answer on May 27, 2026 at 11:15 pm

    I was able to open the menu by hooking the onAttachedToWindow() event. This would fire automatically after the view was created. I used the following code:

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            ((Activity) this).openOptionsMenu();        
        } catch (Exception ex) {
            Log.e("ERR", "Error: " + ex.getMessage());
        }
    }
    

    When I attempted to open the Option Menu in the “onCreate(…)” or “onPostCreate(…)” events, I received the following error: “Unable to add window — token null is not valid; is your activity running?” It seems that the Option Menu is not available until the view is being drawn and registered to the Window.

    As an aside, it may be preferable to open the Options Menu by hooking the “onTouchEvent(Motion event)” as in:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ((Activity) this).openOptionsMenu();
        return super.onTouchEvent(event);
    }
    

    This way, the Options Menu is displayed if the user taps or swipes the activity. To round out the discussion, the Option Menu may be dismissed via the following command:

    ((Activity) mContext).closeOptionsMenu();

    Thus, the Options Menu can be “toggled” by using the following commands:

        ((Activity) this).openOptionsMenu();
        ((Activity) this).closeOptionsMenu(); 
    

    Well, I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ordinary activity with Option Menu, which I created by this code: @Override
Is it possible to use C/C++ code in Android? And how easy is it?
I need directions on how to start the android browser through code . Thanks
I have this code on my Android phone. URI uri = new URI(url); HttpPost
When I add this code to my Android 2.1 Java app, it fails: db=SQLiteDatabase.openOrCreateDatabase(Locations,
I am using the Android code below to connect to Facebook but getting the
this Android code worked fine before, but i'm having problems for some reason. here
In this code I keep getting an error, I know its how I'm saving
While compiling the c++ code using android NDK I am getting the following error:
hi all i am implementing android code for getting the contacts from contact list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.