0

I'm trying to add a top toolbar, with an options menu in it, in a fragment. Upon running it on the emulator the toolbar doesn't show up.
I have called setSupportActionBar(toolbar), still not sure what's wrong.
EDIT: I've already changed the app theme to NoActionBar.

ProfileFragment.java

public class ProfileFragment extends Fragment {

private static final String TAG = "ProfileFragment";

private static final int ACTIVITY_NUM = 4;


private Toolbar toolbar;
private BottomNavigationViewEx bottomNavigationView;

private Context mContext;

//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);

    toolbar = view.findViewById(R.id.profiletoolbar);
    bottomNavigationView = view.findViewById(R.id.bottomnav);
    mContext = getActivity();
    Log.d(TAG, "onCreateView: started.");

    setupBottomNavigationView();
    setupToolBar();

    return view;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
}

//bottom nav setup
public void setupBottomNavigationView() {

    Log.d(TAG, "setupBottomNavigationView: starting bottomnavsetup");
    BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationView);
    BottomNavigationViewHelper.enableBottomNav(mContext, bottomNavigationView);
    Menu menu = bottomNavigationView.getMenu();
    MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
    menuItem.setChecked(true);
}

//toolbar setup
private void setupToolBar() {
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

    if (item.getItemId() == R.id.signout) {
        Toast.makeText(mContext, "Sign out clicked", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onOptionsItemSelected: attempting to sign out");
        mAuth.signOut();
        getActivity().finish();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    menuInflater.inflate(R.menu.profile_menu, menu);
    super.onCreateOptionsMenu(menu, menuInflater);
}
}

snippet_top_profile_bar.xml (This is the toolbar)

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/white_grey_border_bottom">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profiletoolbar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="#username"
                android:id="@+id/username"/>
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

I have all the necessary imports.

1 Answers1

0

This method will work in Activity. But, here you are trying to do it with Fragment. fragments are sub activities so they have limited scope to do things.

Another thing that may happen, You should change the Activity theme to noActionbar. By using this, default toolbar will be hide your custom toolbar will be displayed.

D. PRAKASH
  • 305
  • 1
  • 12
  • According to [this](https://stackoverflow.com/a/38189630/8521243), I should be able to do it in fragments as well. I have already changed it NoActionBar which I forgot to mention. – Ritesh Sachan Jun 09 '20 at 12:36