1

I'm trying to figure out how to have the title of a toolbar on top of the menu but I don't find anything online.

basically I want to have the same toolbar as google maps on that screenshot at the bottom: their app but at the moment I only have this: my app

Here is how I add menu in my toolbar

    toolbar = this.findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.menu.a_menu);
    toolbar.setTitle("Title");

Here is the XML for my toolbar

    <androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:layout_gravity="bottom"
    android:layout_margin="5dp"
    android:animateLayoutChanges="true"
    android:background="#fff"
    android:elevation="8dp"
    android:gravity="top|left"

    ></androidx.appcompat.widget.Toolbar>
nell
  • 11
  • 1

1 Answers1

1

I guess you can use a custom title by adding a TextView within the Toolbar. That actually won't solve the problem, but it will allow you to add your menu below this TextView by using custom layout within the Toolbar.

and to add the menu in certain place within the Toolbar, you can use ActionMenuView, and add the menu programmatically within onCreateOptionsMenu() activity callback, and handle menu clicks with onOptionsItemSelected

MainActivity

public class MainActivity extends AppCompatActivity {

    private ActionMenuView mMenuView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Inflating ActionMenuView
        mMenuView = findViewById(R.id.menu_view);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Adding menu to the ActionMenuView
        getMenuInflater().inflate(R.menu.a_menu, mMenuView.getMenu());
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // handle menu clicks
        return super.onOptionsItemSelected(item);
    }

}

Your Toolbar

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_margin="5dp"
    android:animateLayoutChanges="true"
    android:background="#fff"
    android:elevation="8dp"
    android:gravity="top">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/toolbar_title"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            android:text="3 E Main Street" />

        <androidx.appcompat.widget.ActionMenuView
            android:id="@+id/menu_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</androidx.appcompat.widget.Toolbar>

How it looks

enter image description here

Zain
  • 37,492
  • 7
  • 60
  • 84
  • 1
    I tried this and it worked except that I had trouble intercepting clicks until I began experimenting with this underdocumented MenuBuilder API. https://stackoverflow.com/questions/27988346/how-to-use-actionmenuview – John Gorenfeld Oct 06 '20 at 11:26