I'm making a Bottom Navigation View
.
Show fragments when switching screens.
However, when switching to Fragment, the icon
at the top right (Option menu
) is not visible.
As a solution for similar reasons, I added setHasOptionsMenu(true)
, but the options menu is still not visible.
MainActivity.xml
<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"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottom_nav"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment.java
public class WorkoutListFragment extends Fragment {
RecyclerView rcv_dailyRecord;
TextView mainNotifyText;
Toolbar toolbar;
LinearLayoutManager lm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_workout_list, container, false);
initViews(rootView);
setHasOptionsMenu(true);
return rootView;
}
public void initViews(View v) {
rcv_dailyRecord = v.findViewById(R.id.rcv_dailyrecord);
toolbar = v.findViewById(R.id.toolbar);
mainNotifyText = v.findViewById(R.id.main_notification_text);
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.record_item_add, menu);
}
}
fragment.xml
<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"
tools:context=".activity.WorkoutListActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="@style/Theme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="LIST"
app:titleTextColor="@color/black"
app:titleMarginStart="30dp"
android:paddingRight="30dp"
android:theme="@style/Theme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<TextView
android:id="@+id/main_notification_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CHOOSE WORKOUT"
android:textSize="16dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/orgin_text_color"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_dailyrecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:scrollbars="vertical" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
menu.xml
<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">
<item
android:icon="@drawable/ic_action_add"
android:title="A D D"
app:showAsAction="always" />
</menu>
Try with this because you have a custom toolbar in your Fragment,
just add your toolBar into your activity and setHasOptionMenu(true)
.
add this in your onCreate method:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_blank, container, false);
initViews(rootView);
((AppCompatActivity) requireActivity()).setSupportActionBar((toolbar));
setHasOptionsMenu(true);
return rootView;
}