-2

I'm just starting with android using android studio.

I have created a new project and made some minimal changes based on codes found in a few websites and when I run it, I see the app but I do not see the menu (three dots top to bottom).

I understand that there is supposed to be a menu button which on clicked would show menu but is there a way to ensure that the menu will be shown in the top bar in any case.

menu_main.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/action_settings" android:title="@string/action_settings"
         app:showAsAction="never" />
    <item android:id="@+id/action_other" android:title="@string/action_other"/>
<item android:id="@+id/action_exit" android:title="@string/action_exit"/>

</menu>

MainActivity:

package com.example.arnab.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        boolean handled=false;

        int id = item.getItemId();

        /*//noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);*/

        switch (id){
            case R.id.action_other:
                onClickMenuOther(item);
                handled =true;
                break;
            case R.id.action_exit:
                onClickMenuExit(item);
                handled =true;
                break;
            default:
                handled=super.onOptionsItemSelected(item);
        }

        return handled;
    }

    public void onClickMenuOther(MenuItem item){
        Toast toast= Toast.makeText(this, "Other Click", Toast.LENGTH_LONG);
        toast.show();
    }

    public void onClickMenuExit(MenuItem item){
        finish();
    }

}

Thanks

Arnab
  • 2,324
  • 6
  • 36
  • 60

2 Answers2

0

In the xml, change showAsAction="never" to always or ifRoom.

albertoqa
  • 525
  • 3
  • 11
  • It shows all the menu items in text in the bar, is it not possible to ensure that only three dots are shown and on click or on hover of that, all the three text is viewed one below the other – Arnab Jun 30 '15 at 07:37
0

An answer to this solved my problem:

menu_main.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/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_overflow"
    android:title="">
    <menu>
        <item android:id="@+id/action_other" android:title="@string/action_other"/>

        <item android:id="@+id/action_exit" android:title="@string/action_exit"/>


    </menu>
</item>
</menu>
Community
  • 1
  • 1
Arnab
  • 2,324
  • 6
  • 36
  • 60