Skip to content
Advertisement

Why click on optionsMenu item doesn’t work?

I have a problem when clicking on an item from an options menu. I’ve read how to create a menu on the website https://developer.android.com/guide/topics/ui/menus and I seem to do it right, but it still doesn’t work. The menu exists, it is visible, but nothing happens when I perform the click. Also, there are no errors in the Logcat.

The XML file for the menu is:

<?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:title="@string/edit"
        android:id="@+id/edit_menu_script_item"
        app:showAsAction="always"
        android:icon="@drawable/ic_edit"/>
    <item android:title="@string/delete"
        android:id="@+id/delete_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/share"
        android:id="@+id/share_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/scenes"
        android:id="@+id/scenes_menu_script_item"
        app:showAsAction="never"/>
    <item android:title="@string/characters"
        android:id="@+id/characters_menu_script_item"
        app:showAsAction="never"/>

</menu>

EDIT: Neither of the Toasts messages are displayed and the menu closes after performing click on an item.

The menu has one item on the toolbar, and the others in the dropdown menu: menu_on_toolbar dropdown_menu

As requested, here is the XML code for my activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ViewProjectSW">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbarViewProjectSW"
        app:title="@string/view_your_script"
        app:menu="@menu/menu_script"
        android:background="@color/barColor"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/textBodyColor"
        android:hint="@string/script_here"
        android:id="@+id/scriptTV"/>
</LinearLayout>

and the java class:

package com.stud.scriptreality;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.stud.scriptreality.classes.Screenwriter;
import com.stud.scriptreality.classes.Script;

public class ViewProjectSW extends AppCompatActivity {

    Toolbar toolbar;
    TextView scriptTV;
    Intent intent;
    String title;
    Script script;
    Screenwriter screenwriter = new Screenwriter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_project_sw);

        initialization();

    }

    public void initialization(){
        intent=getIntent();
        title=intent.getStringExtra("title");
        int id= intent.getIntExtra("position",0);
        screenwriter = intent.getParcelableExtra("autor");

        toolbar=findViewById(R.id.toolbarViewProjectSW);
        toolbar.setTitle(title);
        script = new Script(id,screenwriter,title);

        scriptTV=findViewById(R.id.scriptTV);
        scriptTV.setClickable(false);
        Toast.makeText(this, R.menu.menu_script+"*", Toast.LENGTH_SHORT).show();
    }

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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Toast.makeText(this, item.getItemId(), Toast.LENGTH_SHORT).show();

        switch (item.getItemId()){
            case R.id.edit_menu_script_item:
                Toast.makeText(this, "Opening edit page...", Toast.LENGTH_SHORT).show();
                Intent it = new Intent(getApplicationContext(),EditScript.class);
                it.putExtra("script",script);
                startActivity(it);
                break;
            case R.id.characters_menu_script_item:
                Toast.makeText(this, "Opening characters list...", Toast.LENGTH_SHORT).show();
                Intent it2 = new Intent(getApplicationContext(),ViewCharactersSW.class);
                startActivity(it2);
                break;
            case R.id.delete_menu_script_item:
                Toast.makeText(this, "Deleting script...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.scenes_menu_script_item:
                Toast.makeText(this, "Opening scenes list...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.share_menu_script_item:
                Toast.makeText(this, "Opening sharing dialog...", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

}

I don’t use the ActionBar, but another Toolbar I created, and I had set for the entire app a style with NoActionBar. <style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">

Thanks in advance!

Advertisement

Answer

Try add this line in your initialization method:

setSupportActionBar()

Well, you need to set support action bar setSupportActionBar(); and pass your toolbar variable, like this:

setSupportActionBar(toolbar);

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement