0

I'm a new one in Android. I have some problems with showing menu. I don't see three dots in right corner in my screen. Please, help me to understand my mistake. THANK YOU A LOT!

Activity:

public class MainActivity extends AppCompatActivity  {

    private EditText numb1;
    private EditText numb2;
    private Button btn_sum; 
    private Button btn_extr;
    private Button btn_mult;
    private Button btn_div;     
    private TextView result;        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*some code*/
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.reset:
                numb1.setText("");
                numb2.setText("");
                break;
            case R.id.exit:
                fileList();
                break;
        }   
        return super.onOptionsItemSelected(item);
    }   
}  

xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/reset"
        android:title="@string/reset"
        app:showAsAction="never"/>
    <item android:id="@+id/exit"
        android:title="@string/exit"
        app:showAsAction="never"/>
</menu>
Dat Nguyen
  • 1,626
  • 22
  • 25
NewOne
  • 1
  • 1

6 Answers6

0

Menu Items are not showing on Action Bar

Check this answer

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        android:title="@string/action_option1"/>
    <item
        android:id="@+id/action_settings34"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        android:title="@string/action_option2"/>
    <item
        android:id="@+id/action_settings3"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        android:title="@string/action_option3"/>

</menu> 
Nikul Rao
  • 83
  • 8
0

Use this code in your activity, but you should have action bar in it.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
0

if you run your application on oldest version of samsung or other the three DOTS is not appear on ActionBar

so try to click on Option Key on mobile

The solution to appear Three DOTS

  • call this method in your application class' onCreate method

    private void makeActionOverflowMenuShown() {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        Log.d(TAG, e.getLocalizedMessage());
    }
    }
    
ND1010_
  • 3,743
  • 24
  • 41
0

i am also new to android i guess u wrote onCreateOptionsMenu(Menu menu) inside on create try this

public class MainActivity extends AppCompatActivity {

private EditText numb1;
private EditText numb2;
private Button btn_sum;
private Button btn_extr;
private Button btn_mult;
private Button btn_div;
private TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
/*some code*/


}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.reset:
            numb1.setText("");
            numb2.setText("");
            break;
        case R.id.exit:
            fileList();
            break;
    }
    return super.onOptionsItemSelected(item);
}

}

Rajkiran
  • 5
  • 7
0

You have done the minor mistake while creating the option menu .you should call the onCreateOptionsMenu() and onOptionsItemSelected() method outside the onCreate(Bundle savedInstanceState) method. you may check the following example :

public class MainActivity extends AppCompatActivity {

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.reset:
                break;
            case R.id.exit:
                fileList();
                break;
        }
        return super.onOptionsItemSelected(item);
    } }
PRIYA PARASHAR
  • 777
  • 4
  • 15
0

Help me to understand my mistake - Sure

The Main culprit is in your menu XML file app:showAsAction="never" this line replace this line with app:showAsAction="ifRoom"

here showAsAction is set to never means you tell that don't show my menu in action bar if you replace with "ifRoom" means you said show my all menu in action bar and if there is space for all my menus

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55