rnng
rnng

Reputation: 111

Aligning items of optionsmenu (in ActionBar)

I want to align one single menu-item from my optionsmenu on the left, as you can see in the contacts-app (while editing contacts). What does I have to do? Does I have to change thy xml attributes or do it in the Java-Code?

Best regards, rnng

Screenshot of contacts (editing-) activity

Upvotes: 3

Views: 2361

Answers (2)

rnng
rnng

Reputation: 111

I looked up the source code of the contacts-app from here.

The code says that I have to create an Custom Layout for the ActionBar with this XML-code:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/save_menu_item"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:divider="?android:attr/dividerVertical"
        android:showDividers="end"
        android:dividerPadding="12dip"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:duplicateParentState="true"
            style="?android:attr/actionButtonStyle">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="8dip"
                android:src="@drawable/ic_menu_done_holo_dark"
                android:description="@string/menu_done" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="20dip"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/action_bar_button_text_color"
                android:text="@string/menu_done"
                style="@android:style/Widget.Holo.ActionBar.TabText" />

        </LinearLayout>

    </LinearLayout>

</FrameLayout>

And this Java code here:

 // Inflate a custom action bar that contains the "done" button for saving changes
    // to the contact
    LayoutInflater inflater = (LayoutInflater) getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
    View customActionBarView = inflater.inflate(R.layout.editor_custom_action_bar, null);
    View saveMenuItem = customActionBarView.findViewById(R.id.save_menu_item);
    saveMenuItem.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        mFragment.doSaveAction();
    }
    // Show the custom action bar but hide the home icon and title
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                    ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME |
                    ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.setCustomView(customActionBarView);

The following files were used:

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1006799

This might be using an ActionMode, which has this sort of look, including the checkmark-on-button on the left.

Or, this might be using setDisplayUseLogoEnabled() and the android:logo attribute on <application>.

Or, this might be using a custom UI and not using the normal action bar.

Upvotes: 2

Related Questions