Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ActionBar home button to right side

Can I set the home button in an ActionBar to the right side? (android.R.id.home)

I want to change the position of home button because the language is using right to left typing.

Is it possible? If yes, please tell me how I can do that?

If no, how can I set ActionBarDrawerToggle at right side?

like image 764
user1571714 Avatar asked Jul 24 '13 15:07

user1571714


1 Answers

You can create such action bar, but it's little more complicated than inflating a menu. Menu created in onCreateOptionsMenu() method will be always aligned to right, placed in split action bar or hidden under the menu key.

If you want your action bar to contain just two menu items - one on the left edge and the other on the right edge - you have to create custom view in the action bar.

The custom view layout will be something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
    <ImageButton android:src="@drawable/ic_menu_item1"
                 android:background="?attr/actionBarItemBackground"
                 android:layout_width="?attr/actionBarSize"
                 android:layout_height="match_parent"
                 android:scaleType="centerInside"
                 android:id="@+id/item1"
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true"/>

    <ImageButton android:src="@drawable/ic_menu_item2"
                 android:background="?attr/actionBarItemBackground"
                 android:layout_width="?attr/actionBarSize"
                 android:layout_height="match_parent"
                 android:scaleType="centerInside"
                 android:id="@+id/item2"
                 android:layout_alignParentRight="true"
                 android:layout_alignParentTop="true"/>
</RelativeLayout>

Define in the theme that you want to use custom view. The theme should contain:

<style name="MyTheme" parent="@style/Theme.Sherlock">
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
</style>

<style name="MyActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
    <item name="displayOptions">showCustom</item>
    <item name="android:displayOptions">showCustom</item>
</style>

Set the custom view in the activity (or fragment):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar ab = getSherlock().getActionBar();
    LayoutInflater li = LayoutInflater.from(this);
    View customView = li.inflate(R.layout.my_custom_view, null);
    ab.setCustomView(customView);

    ImageButton ibItem1 = (ImageButton) customView.findViewById(R.id.item1);
    ibItem1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // ...
        }
    });

    ImageButton ibItem2 = (ImageButton) customView.findViewById(R.id.item2);
    ibItem2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // ...
        }
    });
}
like image 137
Петър Петров Avatar answered Oct 01 '22 21:10

Петър Петров