Here's my activity_main.xml, which have a basic Toolbar and two Buttons on the right (and as well a NavigationView):
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="com.myapp.MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
android:orientation="vertical"
android:keepScreenOn="true"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="horizontal">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<Button
android:id="@+id/btnRandom"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_shuffle">
</Button>
<Button
android:id="@+id/btnRescanSongs"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@android:drawable/stat_notify_sync">
</Button>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
// other stuff
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
I set it on MainActivity this way:
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Now, on another Activity (SyncActivity), I'd like to display the same Toolbar but with different icons on the right.
What's the correct way to do this?
Doing this on activity_sync.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
tools:context=".SyncActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
android:orientation="vertical"
android:keepScreenOn="true"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="horizontal">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<Button
android:id="@+id/btnRescanSongs"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@android:drawable/stat_notify_sync">
</Button>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<ListView
android:id="@+id/log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:padding="8dp">
</ListView>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and on SyncActivity:
setContentView(R.layout.activity_sync);
Objects.requireNonNull(getSupportActionBar()).setTitle("Sync");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
it adds a new Toolbar, below the main one.
Also: why the icons on main Toolbar are not showed on other Activity? Seems it hides them? Why?
You can remove all the toolbar buttons from the layout xml and move them to menu xml like that.
File:
res/menu/yourmenuname.xmlNote that you also have to provide a title and not only the icon you can use the showAsAction with
alwaysto display always the icon orifRoomorifRoom|withTextjust experiment what likes you most.Then in each of your activities you override this function to load your menu. Pay attention to
yourmenunamethat will be different in each one of the activities.And to handle the menu actions use:
In the second activity use the different menu with the different options and do the handling and creation in a similar way.
There are a lot of stuff you can do with the menus, adding here some links that may help you keep reading on them: