9

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?

Geobits
  • 22,218
  • 6
  • 59
  • 103
user1571714
  • 305
  • 2
  • 5
  • 12
  • Home button?.. Its usually a hardware button, which obviously cannot be moved, and if its a software button, it cannot be changed. Not to mention, its in the middle anyway, so reversing it would still be in the middle. You have most likely not explained what it is you are doing, properly. – IAmGroot Jul 24 '13 at 15:56
  • i mean home button ( android.R.id.home ) – user1571714 Jul 24 '13 at 15:58

5 Answers5

1

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) {
            // ...
        }
    });
}
0

Android has recently gained support for a right-to-left ActionBar, but it is only available in API 17. If your manifest has android:supportsRtl="true" set, it will do this by default.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • my application and i set android:supportsRtl="true" – user1571714 Jul 24 '13 at 16:02
  • In that case, users on API 17 will see it right-to-left, and users <17 will see it the default way. – Geobits Jul 24 '13 at 16:03
  • it doesn't change , – user1571714 Jul 24 '13 at 16:06
  • And you're testing on 17? Odd, since this is exactly what it's supposed to do. http://developer.android.com/about/versions/android-4.2.html#RTL – Geobits Jul 24 '13 at 16:09
  • @Geobits: I have created new emulator of API 17, though it is not working in it. The home icon is on the left side only, but I want it on right. My drawer is opening from right but home icon is not getting set on right side. – Mitesh Shah May 31 '14 at 10:20
0

You have to use your customize layout by calling actionbar.setDisplayShowCustomeEnable(True) and then inflate your custom view and then actionbar.setCustomeView(view)
this can help you (ActionBar logo centered and Action items on sides)

Community
  • 1
  • 1
Sadegh
  • 2,669
  • 1
  • 23
  • 26
0

Not completely sure if this is what you're looking for but you could get the home button to appear under a drop down menu on the action bar (3 vertical dots icon)

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

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/ic_home"
            android:orderInCategory="1"
            android:showAsAction="never"
            android:title="Home"/>

</menu>

By setting it as " android:showAsAction="never" " it will appear under the drop down menu however you will probably not be able to see this in the Android Studio emulator (well at least I've never seen it on the emulator for Android Studio), not sure about Eclipse.

Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
0

"because the language is using right to left typing..." an straight forward answer is to make a theme and use it in manifest:

in style.xml i have made a theme which inherits The AppTheme.

<style name="StoreTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:layoutDirection">rtl</item>
</style>

and in the manifest.xml you can use this theme:

<activity
        android:name=".store.ui.Product.ProductMainActivity"
        android:label="@string/title_activity_product_details"
        android:theme="@style/StoreTheme"/>

or you can make your entire application to use this theme in AndroidManifest.xml application tag:

<application
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
Abilogos
  • 4,777
  • 2
  • 19
  • 39