'Noticeable delay when clicking on options menu
I've always noticed a delay in my apps when I click the options button in a toolbar. It will maybe be a second or so, but it's very obvious compared to other Android apps such as Firefox, where it feels like the options menu inflates instantly as my finger pulls up, so I know it's not just my device. I put together a minimal app, with one activity and 2 layouts:
package com.test.test_option_menu;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
activity_main.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.test.test_option_menu.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout>
And menu.xml 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"
android:layout_width="wrap_content">
<item
android:id="@+id/action_help"
android:title="Help"
app:showAsAction="never" />
</menu>
I still see the momentary delay when clicking the options menu however! How do I get it to be as quick as other apps that I use? I've tried it with the toolbar included in the layout as in here, as well as creating a separate toolbar.xml that gets
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
But I still see the same thing. Is there a more lightweight way of making toolbars? Or pre-loading the menu layout?
Solution 1:[1]
It worked for me to inflate the menu in the toolbar of my layout and not in the action bar of the activity.
In my activity I have:
supportActionBar!!.hide()
val actionBar = view.findViewById<Toolbar>(R.id.toolbar)
actionBar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.xxx-> {
}
}
return@setOnMenuItemClickListener true
}
In my activity layout I have:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:menu="@menu/menu_searchview"
app:title="..." />
In my menu I have:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/searchView"
android:icon="@drawable/ic_search_black_24dp"
android:title="@string/..."
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chuy Porras |
