Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Place ActionBar Items in Main ActionBar and Bottom Bar

Tags:

enter image description here

The Google+ app has a layout where it has action bar items in the main action bar and the bottom. Currently I am using android:uiOptions="splitActionBarWhenNarrow" to place items in the bottom bar. How can I place items on both the top and bottom?

like image 397
PolandSpring Avatar asked Feb 17 '12 11:02

PolandSpring


People also ask

How do you add action items to the action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

How will you replace a menu with action bar in Android?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

What is the action bar in Android?

The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users.


2 Answers

Hope my answer is not too late for you.

  1. Use android:uiOptions="splitActionBarWhenNarrow" (this adds stuff into bottom bar).
  2. Create new layout like the below code (this layout will handle all your items on the top bar).

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >
    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <ImageButton
        android:id="@+id/action_starred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/actionButtonStyle"
        android:src="@android:drawable/ic_menu_compass" 
        android:onClick="FakeMenu"/>
    </LinearLayout>
    
  3. Paste this in your activity

    ActionBar actionBar = getActionBar();               
    actionBar.setCustomView(R.layout.actionbar_top); //load your layout
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_CUSTOM); //show it 
    
  4. That's all :)

like image 181
Pavel Sikun Avatar answered Oct 02 '22 19:10

Pavel Sikun


I don't think it is possible using the standard ActionBar. When enabling a split ActionBar, ALL actions will appear at the bottom on a narrow screen.

The bottom bar in the Google+ app's "create a post" Activity seems to be a custom implementation. Notice the long press to reveal an action's label does not work, and the bottom bar remains even when you switch to landscape orientation. The location item is a toggle switch which is also non-standard ActionBar behavior.

like image 32
Jeff Gilfelt Avatar answered Oct 02 '22 19:10

Jeff Gilfelt