I understand similar questions have previously been asked, but for some reason despite having tried multiple solutions, I am not able to get this to work.
Basically I have created a toolbar in my fragments XML file and put all its components into the items menu file (bottom_navigation_menu). However when I click on the items in the menu, they are getting no response. I tried adding a log.d, to see if the setnavigationonclick function is being called and it isn't. I have attached my code below.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
public class pneumothorax_fr_flashcard_view extends Fragment {
private static final String TAG = "flashcard view";
private pneumothorax_layout_flashcard_view customView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
customView = (pneumothorax_layout_flashcard_view) view.findViewById(R.id.flashcardView);
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbarBot);
((AppCompatActivity) getActivity()).getSupportActionBar();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customView.swapColor();
}
});
return view;
}
}
Here is the XML, in this file I have both a toolbar and a custom view which has a green box, the colour of that box is meant to change when onclick is performed.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toolbarBot"
app:menu="@menu/bottom_navigation_menu">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="wrap_content">
<com.example.spidermed.pneumothorax_layout_flashcard_view
android:id="@+id/flashcardView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</RelativeLayout>`
You have to use setOnMenuItemClickListener
to handle menu items. setNavigationOnClickListener
is only for the navigation button on the left hand side (which only displays if you call setNavigationIcon()
to set an icon for it).