Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to make an Action bar Menu for my page in android studio. but when i run the app there is no Action bar.
i write the menu XML codes in menu_main.xml like this:
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

    <item
    android:id="@+id/SigningUp"
            showAsAction="ifRoom|withText"
         android:title="ثبت نام"/>
   </menu>


and this is my java code:
Java
    public static final int SIGN_UP_ID = Menu.FIRST;
    public static final int EXIT_APP_ID = Menu.FIRST + 1;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        menu.add(0, SIGN_UP_ID, 0, R.string.sign_up);
        menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);


        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch(item.getItemId())
        {
            case SIGN_UP_ID:
                Intent i = new Intent(LoggingIn.this, signingUp.class);
                startActivity(i);
                return true;
            case EXIT_APP_ID:

                return true;
        }

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

1 solution

According to the documentation[^],
Quote:
onCreateOptionsMenu (Menu menu) initializes the contents of the Activity's standard options menu. You should place your menu items in to menu. This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).

onPrepareOptionsMenu (Menu menu)[^]
So, place add menu item code inside onPrepareOptionsMenu(Menu).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900