Action bar not shown in 4.2.2

SOLUTION:. This was a problem with the image search icon. Android did not find images for the search action, I did not add them to all res-folders, it started showing after that ...

I am trying to add an action bar to the application and I am following the basic tutorial shown on the google developer website

I wrote the following code.

Mainactivity

public class MainActivity extends ActionBarActivity { public void openSearch(){ System.out.println("TEST SEARCH"); } public void openSettings(){ System.out.println("TEST SETTINGS"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.action_search: openSearch(); return true; case R.id.action_settings: openSettings(); return true; default: return super.onOptionsItemSelected(item); } } } 

Res / menu / main_activity_actions.xml

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showAsAction="ifRoom" /> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /> </menu> 

AndroidManifest.xml

 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sample.actionbarsample.MainActivity" android:label="@string/app_name" android:theme="@style/Theme.AppCompat" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

But instead of an action bar such as

enter image description here

I get the options menu. What is displayed when I press the menu button during application launch.

Screen Shot on Emulator running 4.2.2

What am I missing from the tutorial, or is there something wrong with the code?

EDIT

After doing what the beworker asked me, I got this result, still without a search box.

enter image description here

+3
source share
2 answers

Nothing wrong with the code or the tutorial. All about customizing your emulator. You need to configure the emulator so that it has soft buttons. Go to the "Device Definition" tab and edit the device that uses the "Software" buttons, as shown in the figure below.

enter image description here

Alternatively, you can simply disable the "Keyboard / Hardware keyboard present" property in the ADV configuration (see the figure below) and restart the emulator.

enter image description here

Android 4.0 emulates an old-style menu if a hardware menu is present. That is why all menu actions will take place. If you disable the hardware menu in the emulator, then the actions will go to the action bar, as expected.

Please note that on phones with hardware menu buttons, such as the Samsung G2, G3 popup menu, etc., they will still be displayed, and you cannot change this in your code. Android will decide what is best for users on each device.

Update

One more note. If you use the compatibility library, make sure you use xmlns:yourapp="http://schemas.android.com/apk/res-auto" and yourapp:showAsAction="always" , as described below.

However, if your application uses the support library for compatibility with the version with Android 2.1, the showAsAction attribute is not accessible from the android: namespace namespace. Instead, this attribute is provided by the Support Library, and you must define your own XML namespace and use this namespace as the attribute prefix.

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="always" /> ... </menu> 
+8
source

What am I missing from the tutorial, or is there something wrong with the code?

Yes, there is a difference. Try to remove the sample from your project.

In fact, your project name occupies all the space and in the search menu item you asked the system to provide space if there is available space / space that is not associated with a long project name (maybe activity or something else) in your case.

So, either change the name of your project to something small, or use "always" as follows:

  <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showAsAction="always" /> 
+1
source

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


All Articles