6

I am trying to push the back icon image on the left side of the app bar in the android but the icon displaying in the middle of app(tool) bar. How to align icon on the right side of the app bar in android? Please have a look at the image below.

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="divine.calcify.activities.HomeScreenActivity">
    <item
        android:layoutDirection="ltr"
        android:id="@+id/hs_back_button"
        android:icon="@mipmap/ic_back"
        android:title="@string/homescreen_back"
        app:showAsAction="always"
        ></item>
    <item
        android:id="@+id/menu_search"
        android:icon="@mipmap/ic_search"
        android:title="@string/homescreen_search"
        android:orderInCategory="1"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item android:id="@+id/menu_notifications"
        android:icon="@mipmap/ic_divine_notification"
        android:title="@string/homescreen_notification"
        android:orderInCategory="2"
        app:showAsAction="always" />
</menu>

enter image description here

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Mithun Kumar
  • 595
  • 1
  • 9
  • 18

2 Answers2

1

Pragmatically add back button in java file (in Activity)

Try this

 ActionBar actionBar = getSupportActionBar();
 actionBar.setDisplayHomeAsUpEnabled(true);
 actionBar.setTitle("Divine");
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
0

You must use Toolbar instead of ActionBar. Toolbar gives you freedom to design it freely as normal view.

If you dont have implemented toolbar yet no problem.

  1. Extend you class to ActionBarActivity
  2. Update theme with parent Theme.AppCompat.Light.NoActionBar

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <!--<item name="colorPrimary">@color/colorPrimary</item>--> <!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>--> <!--<item name="colorAccent">@color/colorAccent</item>--> </style>

  3. Create your toolbar layout.

     <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:minHeight="100dp"
        android:background="@color/colorPrimary">
    
    
        <!--your views here-->
     </android.support.v7.widget.Toolbar>
    
  4. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

Full Demo Here

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41