I did it by using these steps.
onCreateView
of the main fragment.((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Your title");
MainActivity
(Parent Activity) of the fragment. Even if you are using any button or menu item then you can change the title from onSelectedItemClickListener
, just like i did in my case.
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.menu_dashboard:
getSupportActionBar().setTitle("Dashboard");
fm.beginTransaction().hide(active).show(dashboardFragment).commit();
active = dashboardFragment;
return true;
case R.id.menu_workshop:
getSupportActionBar().setTitle("Workshops");
fm.beginTransaction().hide(active).show(workshopFragment).commit();
active = workshopFragment;
return true;
}
return false;
}