SoFunction
Updated on 2025-03-02

Android   Detailed explanation of the operation of ActionBar control

ActionBar is a control displayed at the top of the screen. It includes the app's logo icon displayed on the left and the visible items of the action menu on the right.

Implementation method

The icon on the ActionBar is called ActionButtons, which can place unimportant ActionButtons in ActionOverflows.

<menu xmlns:andro >
    <item
        android:
        android:actionProviderClass=""
        android:showAsAction="ifRoom"
        android:title="@string/share"/>
    <item
        android:
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom"
        android:title="@string/search"/>
    <item
        android:
        android:showAsAction="always"
        android:title="@string/setting"/>
</menu>

In Activity:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        MenuItem shareItem = (.action_share);
        return true;
    }

Custom ActionBar background:

  • Create a new custom style in the process to inherit the existing Action Bar Style()
  • Rewrite its actionBarStyle property
  • The actionBarStyle property value points to another Style whose background property has been overwritten
  • Specify the property value of the background

:

<resources xmlns:andro>
    <style name="CustomActionBarTheme" parent="@android:style/">
        <item name="android:actionBarStyle">@style/CustomBackground</item>
    </style>
    <style name="CustomBackground" parent="@android:style/">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

Set ActionBar to Tab style:

ActionBar actionBar = getActionBar(); //for <3.0 getSupportActionBar();
        (ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 3; i++){
            Tab tab = ();
            ("Tab" + i);
            (null);
            (tab);
        }

Implement the Tablistener callback method:

   tabListener = new () {
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            (, "TabSelected" + (), Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    };

Enable Overlay mode

ActionBar takes up a certain amount of screen space and can be automatically hidden, but every time it automatically hides, it will cause the screen to be recalculated. You can set it to Overlay mode to place ActionBar on top of the screen instead of on top.

First, you need to create a custom theme and set the property to true.

<style
        name="CustomActionBarOverlayTheme"
        parent="@android:style/"
        >
        <item name="android:windowActionBarOverlay">true</item>
    </style>

If you want to reserve a certain space, you can specify PaddingTop:

android:paddingTop="?android:attr/actionBarSize"

Add ActionProvider:

:

   <item
        android:
        android:actionProviderClass=""
        android:showAsAction="ifRoom"
        android:title="@string/share"/>

In Activity code: (All applications in the current system that can send pictures)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    MenuItem shareItem = (.action_share);
    mShareActionProvider = (ShareActionProvider) ();
    (getDefaultIntent());
    return true;
}
private Intent getDefaultIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    ("image/*");
    return intent;

This is the end of this article about the detailed explanation of Android ActionBar control operation. For more related contents of Android ActionBar controls, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!