简体   繁体   中英

Android ToolBar Menu Item not Clickable

My android app menu item is not clickable I Add app:showAsAction="always" and android:enabled="true" to the menu item and The Item Button is shown but it's not clickable, What Im missing here?

App Theme: parent="Theme.MaterialComponents.DayNight.DarkActionBar"

Layout:

<?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:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:orientation="vertical"
    tools:context=".Settings.ReportTable">

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:id="@+id/toolbar"/>


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/balance_table_SV"/>
</LinearLayout>

Activity:

public class ReportTable extends AppCompatActivity {

    ScrollView balance_table_SV;
    TableLayout tableLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_table);
     
         Toolbar toolbar =  findViewById(R.id.toolbar);
         toolbar.inflateMenu(R.menu.excel);
        balance_table_SV = findViewById(R.id.balance_table_SV);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if(item.getItemId() == R.menu.excel) {
            export_to_excel();
        }

        return super.onOptionsItemSelected(item);
    }
}

First of all, it looks like you're comparing the item ID to the resource ID of the entire menu ( R.menu.excel ). You should have an ID for each menu item to use for click handling:

<item android:id="@+id/excel" />
if(item.getItemId() == R.id.excel) { ... }

Secondly there seems to be quite a bit of confusion here between the Toolbar and the Android action bar. You are you using a mixture of both in the code you have provided which is causing the inconsistency.

Firstly, if you're using a Toolbar in your layout, you don't need the action bar at all, therefore you should be inheriting from a NoActionBar Material Components theme (such as Theme.MaterialComponents.DayNight.NoActionBar ). Then in order to setup your Toolbar you have two options:

Set the Toolbar as the action bar

With this approach you can register the Toolbar to act as the action bar for an activity, which means it correctly triggers the onOptionsItemSelected callback etc. So to do this we need to register it in onCreate :

setSupportActionBar(toolbar);

Then you should probably be using onCreateOptionsMenu to inflate the menu instead of Toolbar.inflateMenu just as you've already done in your code:

getMenuInflater().inflate(R.menu.excel, menu);

And finally to listen for menu item clicks, you can override onOptionsItemSelected and check the item ID as I outlined above.

Setup the Toolbar independently

Instead of registering the Toolbar as the action bar, you can instead just use the Toolbar like any regular view and set it up directly. Firstly to inflate the menu you can call:

toolbar.inflateMenu(R.menu.excel);

Then in order to listen for click events you can attach an OnMenuItemClickListener as follows:

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        ...
    }
});

Both approaches are valid but I personally prefer the second. It seems strange to try and work around the activity lifecycle when you could just treat the Toolbar normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM