7

I am working with the 'AppCompact' library and faced some problems with layout/positioning. I want to position the App icon on the right hand side of the 'ActionBar'. One method is to define a button in the toolbar, but is there a standard method to set the App icon and Up button on the right hand side of the ActionBar?

enter image description here

As you can see in the above image, the icon is positioned on the left, I want it to be on the right. Any help would be appreciated.

P.s:For people who may face my problem it can be fixed easily using this code Add this code to manifest:

<application android:supportsRtl="true">

and then write this code on Oncreate:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Majid Hojati
  • 1,740
  • 4
  • 29
  • 61
  • @ShabbirDhangot so it seems the easiest way is to define a costomized item in toolbar? is there any way to hide app icon?or set its toolbar layout right to left? – Majid Hojati Feb 09 '15 at 11:39
  • I suggest you to go with creating header with layout and adjust your views. hide current actionbar. also If you dont go to that way you can hide action bar logo using style. described here http://stackoverflow.com/questions/14606294/remove-icon-logo-from-action-bar-on-android – Shabbir Dhangot Feb 09 '15 at 11:43
  • excellent, it worked for me. – Hamid Jolany Mar 15 '16 at 19:29

3 Answers3

18

There is no way android provides to set app icon at right side of actionbar but you can still do that.

Create a menu, say main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item"
        android:icon="@drawable/your_app_icon"
        android:title="@string/menu_item"
        app:showAsAction="always"/>  //set showAsAction always
                                    //and this should be the only menu item with show as action always

</menu>

Now just override onCreateOptionsMenu in your activity class.

add this in MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

It's done! Your app icon will be visible on right side of ActionBar now.

If you have more than one item in menu then override onPrepareOptionsMenu in activity class and set setEnabled(false) for menu item having app icon, doing this prevents your icon to be clickable.

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    menu.findItem(R.id.menu_item).setEnabled(false);

    return super.onPrepareOptionsMenu(menu);
}

Now your MainActivity.java file would look like

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
        case R.id.menu_item:   //this item has your app icon
            return true;

        case R.id.menu_item2:  //other menu items if you have any
            //add any action here
            return true;

        case ... //do for all other menu items

        default: return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    menu.findItem(R.id.menu_item).setEnabled(false);

    return super.onPrepareOptionsMenu(menu);
}

This is the only trick you can use to set app icon on right side.

Apurva
  • 7,871
  • 7
  • 40
  • 59
  • thank you for your help..I have find another method and I updated my queston..Do you think the method that I used is wrong?thank you – Majid Hojati Feb 09 '15 at 17:38
  • @MajidHojati The attribute `supportsRtl` was added in api level 17 so it will not work for api < 17. Use it only if your `minSdkVersion` is 17. Source : http://developer.android.com/guide/topics/manifest/application-element.html#supportsrtl – Apurva Feb 09 '15 at 17:48
1

There is no other way than set the layout to RTL or implement it yourself. Even if you subclass Toolbar and try to change onLayout, you would need to recalculate position of all views.

StenSoft
  • 9,369
  • 25
  • 30
0

I just added android:layout_marginStart="300dp" in the xml where the imagebutton is declared.

Ruli
  • 2,592
  • 12
  • 30
  • 40
Shreya J
  • 1
  • 1