android – How to Place ActionBar Items in Main ActionBar and Bottom Bar-ThrowExceptions

Exception or error:

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?

How to solve:

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 🙂

###

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.

###

I had the same problem, nothing that i found was really working because i had to deal with the narrow of the actionbar, then on big devices, buttons were placed on top instead of bottom. So i finally solved it by placing a “footer” in the main layout:

Bottom ActionBar Example

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="false"
        android:layout_above="@+id/footer">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:id="@+id/container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/body_padding_large"
            android:paddingRight="@dimen/body_padding_large"
            android:paddingTop="@dimen/body_padding_medium"
            android:paddingBottom="@dimen/body_padding_medium"
            android:gravity="top">

        </LinearLayout>

    </ScrollView>
    <LinearLayout android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        style="@android:style/ButtonBar">
        <Button android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>
</RelativeLayout>

###

The bottom bar gets filled when you have more items than can be shown in the top bar.

Instead of showing the “overflow” three-dots the items will be put on the bottom bar. Remember to use the:

android:showAsAction="ifRoom|withText"

on your menu items in the XML.

Read more here:
http://developer.android.com/guide/topics/ui/actionbar.html#SplitBar

###

<?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>









//Then put these lines of code in your java class or activity

    ActionBar actionBar = getActionBar();               
    actionBar.setCustomView(R.layout.actionbar); //load your layout
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_USTOM); //show it 

// hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *