How to make such an ActionBar ? Preferably in the form of Java code, rather than XML markup.

alt text

I just started to figure it out and wrote simple code:

 MyActivity extends Activity { @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); ActionBar actionBar = getActionBar(); actionBar.addTab(actionBar.newTab().setText("Test")); } } 

The getActionBar method always returns Null . Why?

  • And what version of api is used? From the docks: "GetActionBar () public ActionBar Since: API Level 11 Retrieve a ActionBar. Returns The Activity's ActionBar, or null if it doesn’t have one." - DroidAlex
  • Judging by the screenshot, there should be at least 11 API-level. - vitaly_gashock
  • I don't know :( - angry

1 answer 1

In fact, no tricks here do not need to do. Due to the fact that the API will enable the API-level> 11, the android will automatically form an action bar. Make out the menu in the usual way

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity, menu); return true; } 

and a menu description file

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /> </menu> 

and you will action bar. The main point here is the different variations of the android: showAsAction attribute. With it, you can customize the display of a separate menu item (menu item, an action bar button with a picture / no picture, etc.) The selection of an item is caught in a standard callback.

And yes, you can find out more here ActionBar on developer.android.com

  • Is the API Level indicated like this? <uses-sdk android: minSdkVersion = "11" /> - angry
  • Yes, that's right - vitaly_gashock