Question

I did this menu and when I click a menu item nothing happens, it doesnt't even shows the default toast! Maan I dont know what is going on!

<?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menuLapiz" android:alphabeticShortcut="l"
        android:icon="@drawable/pencil" />

    <item android:id="@+id/menuCirculo" android:alphabeticShortcut="c"
        android:icon="@drawable/circulo" />

    <item android:id="@+id/menuTriangulo" android:alphabeticShortcut="t"
        android:icon="@drawable/triangulo" />
    <item android:id="@+id/menuCuadrado" android:alphabeticShortcut="s"
        android:icon="@drawable/cuadrado" /> </menu>

DrawActivity.java ... Here I got the menu onOptionsSelected which appereantly is not working

import android.app.Activity
import android.os.Bundle;   
import android.view.Menu;  
import android.view.MenuInflater;  
import android.view.MenuItem;
import android.view.Window;  
import android.view.WindowManager;   
import android.widget.Toast;  `

public class DrawActivity extends Activity {

    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // No Title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        super.onCreateOptionsMenu(menu);
        MenuInflater awesome = getMenuInflater();
        awesome.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.menuLapiz:
            Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
            return true;
        default:
            Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
            return true;
        }
    }}

I have no idea why the onOptionsItemSelected(MenuItem item) method is not working I already tried without the S of Options because I read that suggestion in other post but it did not work.

I'm new to android programming so I really have no clue of what is going on .. any help will be appreciated, thanks a lot!! pd: let me know if you want to see the DrawView.java class

Was it helpful?

Solution

Add a return true; to the end of each case

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.mAdd:
        startActivity(new Intent("com.example.ADD"));
        return true;

    case R.id.mAbout:
        startActivity(new Intent("com.example.ABOUT"));
        return true;

    }
    return false;
}

EDIT: toast code

Toast t = Toast.makeText(getApplicationContext(),
                    "This is a toast",
                    Toast.LENGTH_SHORT);
            t.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top