2

I am calling a new activity from the click of the menu button but my application does not showing any menu button.

So, what is the correct way to show the menu option in the application

This is my menu_main.xml looks like:

<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/action_settings"
    android:title="Login"
    android:orderInCategory="100"
    app:showAsAction="always" />

Is there any error in above code?

This is my java code.But I don't think any error in this code. MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent intent = new Intent(this, Login.class);
                this.startActivity(intent);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
}
playmaker420
  • 1,527
  • 4
  • 27
  • 52

2 Answers2

3

Just remove the following code:

 android:orderInCategory="100" 

The android:orderInCategory attribute dictates the order in which the menu items will appear within the menu when it is displayed. The menu items are arranged from left to right in ascending order of integer value of orderInCategory (i.e. 1,2,3... -> left to right). So in your case it is 100 which is too large.

Aman Goel
  • 3,351
  • 1
  • 21
  • 17
2

You can Remove this line android:orderInCategory="100"

Check #So about android:orderInCategory

Read official guide

http://developer.android.com/intl/pt-br/guide/topics/resources/menu-resource.html

CreateOptionsMenu

Activity will fire a CreateOptionsMenu event when it is ready to display the action bar, recall the action bar used to be called the Options Menu. All the onCreateOptionsMenu event handler has to do is inflate the XML file that defines the menu:

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Your code
        }

Hope this helps you .

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198