ActionBarCompat menu item not showing

Here is my general.xml file

 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> <item android:id="@+id/next" android:title="Next" android:visible="true" android:enabled="true" android:showAsAction="always" android:orderInCategory="1"> </item> <item android:id="@+id/Previous" android:title="Previous" android:visible="true" android:enabled="true" android:orderInCategory="2" android:showAsAction="always"> </item> <item android:id="@+id/star" android:icon="@drawable/ic_action_important" android:enabled="true" android:orderInCategory="0" android:showAsAction="always" android:title="Star" android:visible="true"> </item> </menu> 

Here is my code

 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.general, menu); getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84"))); return true; } 

Now my problem is that menu items do not appear in the action bar. Am I doing something wrong here?

+4
source share
1 answer

showAsAction must be in a different namespace ( yourapp in the example below).

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="ifRoom" /> ... </menu> 

youapp is just a namespace identifier that points to the http://schemas.android.com/apk/res-auto namespace, you can change it to anything. The SDK automatically maps this namespace to your package name (see the list of changes below).

Added support for custom views with custom attributes in libraries. Custom attribute layouts should use the http://schemas.android.com/apk/res-auto namespace URI instead of the URI, which includes the application package name. This URI is replaced by the application defined at build time.

This is necessary to correctly search for attributes that are not available in previous versions of the OS, and are also part of your application package. Note from the documentation :

Note that the showAsAction attribute above uses its own namespace defined in the tag. This is necessary when using any XML-code attributes defined by the support library, since these attributes do not exist in the Android platform on older devices. Therefore, you should use your own namespace as a prefix for all attributes defined by the library support.

+19
source

Source: https://habr.com/ru/post/1501913/


All Articles