-1

I need an Action bar similar to this image.

enter image description here

Action bar should have--"Back button at Left side corner,Title of the activity at center,few menu items at the right side corner of Action bar."

I've tried using Custom Layout this aligns the ImageButton to the left but it is hiding the title.

If I use menu items then the left corner back button is missing..

  1. Custom Layout:

enter image description here

  1. Menu Items:

enter image description here

How could I achieve the Action bar with all these requirements.

EDIT

Answer:

I guess it could be done in 3 steps:

  1. As mentioned in accepted answer add toolbar.setDisplayHomeAsUpEnabled(true);
    to add the back button.
  2. Add Title and sub-title by toolbar.addTitle("text") and toolbar.addSubTitle("text")
  3. for icons to be visible on right side:
    By default menu icons are at right. if you want them to be visible in toolbar then in menu item add android:showAsAction="always".
    If it's value is set to "never" then items are added in those three dots.

Answer (after 4 years)

  1. Back button, title and inside overflow button are addressed earlier.
  2. Now, for icon with text and updating that text programmatically --> app:actionLayout="@layout/filter_icon"

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/clear"
        android:title="Clear"
        app:showAsAction="always" />

    <item
        android:id="@+id/filter"
        android:title="Filter"
        app:actionLayout="@layout/filter_icon"
        app:showAsAction="always" />

    <item
        android:title="Option"
        app:showAsAction="never" />
</menu>

filter_icon.xml

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableStart="@drawable/ic_filter"
    android:text="0"
    android:gravity="bottom"
    android:padding="8dp"
    android:textColor="@color/colorWhite"
    android:textSize="14sp" />

Code

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.filter_menu, menu)
    countView = menu?.findItem(R.id.filter)?.actionView as TextView

    return super.onCreateOptionsMenu(menu)
}

fun updateMenuCount() {
    countView?.text = "${selectedCategoryHierarchy.size} "
}

Result

enter image description here

after update

enter image description here

Prabs
  • 4,923
  • 6
  • 38
  • 59
  • 1
    Researching and trying something by yourself is a good start. – m0skit0 Jul 20 '15 at 11:00
  • @m0skit0 I have not posted this question without searching.I've referred 20-30 answers on SO many tutorials,but didn't find anything relevant. – Prabs Jul 20 '15 at 11:05
  • Really? First hit on first search: http://stackoverflow.com/questions/12883732/how-to-display-custom-view-in-actionbar – m0skit0 Jul 20 '15 at 11:08
  • I have a list of answers that I referred but this one is not among them. – Prabs Jul 20 '15 at 11:30
  • http://www.androidhive.info/2013/11/android-working-with-action-bar/ is one of the helpful link. But achieving the similar actionbar wasn't less than a battle ;) – Prabs May 04 '17 at 05:29
  • 1
    @m0skit0 it was very difficult for a developer who started coding 6 months back. Tough roads. Amazing journey :D – Prabs May 18 '21 at 20:13

1 Answers1

0

Well certainly you don't need to implement a Custom Layout for your requirement. A toolbar is what you need.

I can see from your image that you have given an image for the back button, which is not how it should be done. You just have to mention a parent activity in your manifest file under activity tag like this:

android:parentActivityName="com.example.myapp.MainActivity"

Then in your activity class write this(Instead of ActionBar you can use ToolBar also)

ActionBar actionbar=getActionBar(); actionbar.setDisplayHomeAsUpEnabled(true);

and back button automatically appears. Then you can add menu items which will be shown in right.

See this tutorial for more reference.

Prabs
  • 4,923
  • 6
  • 38
  • 59
rusted brain
  • 1,052
  • 10
  • 23
  • Thank you for the link @deepu513. but it is not my requirement.In that tutorial menu items are added to the toolbar which will be added to right corner by default..I need to add an item at right corner too – Prabs Jul 20 '15 at 10:43
  • Answers consisting of solely of an external link are not good. What happens if the link goes dead? – m0skit0 Jul 20 '15 at 10:55
  • @m0skit0 I know that but i am in office right now and answer was pretty big so i gave him link. – rusted brain Jul 20 '15 at 10:57
  • @deepu513 I will remove downvote when you fix it. – m0skit0 Jul 20 '15 at 10:58
  • @deepu513 Another Query..As you can see the cart image and a number on it(indicates the number of items in cart),I've achieved this by using a layout and separate image and set text on it..[Code snippet](http://paste.ofcode.org/innWkR3xPkeFpRYphU23AC) for this pages the default home up is **not working** – Prabs Jul 20 '15 at 12:44