Home » Android » android – ActionBar Title dynamically change with fragment

android – ActionBar Title dynamically change with fragment

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have 1 Activity with 3 Fragments inside (Home-Login-RestorePass) Initially, HomeFragment shows and the other two are hidden. I want that the ActionBar Title change depending on which Fragment is showing.

I’m trying in my Activity with:

public void setActionBarTitle(String title){
    getSupportActionBar().setTitle(title);
}

@Override
public void onResume() {
    super.onResume();
    // Set title
    setActionBarTitle(getString(R.string.app_name));
}

and the fragments has the same:

@Override
public void onResume() {
    super.onResume();
    // Set title
    ((LoginActivity) getActivity()).setActionBarTitle(getString(R.string.fragment_login));
}

But it doesn’t work. It always show R.string.fragment_login on the title.

I’m using FragmentTransaction for the fragment transition:

btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            HomeFragment homeFragment = (HomeFragment) getFragmentManager().findFragmentById(R.id.fragmentHome);
            LoginFragment loginFragment = (LoginFragment) getFragmentManager().findFragmentById(R.id.fragmentLogin);
            ft.hide(homeFragment).addToBackStack(null);
            ft.show(loginFragment).addToBackStack(null).commit();
        }
    });

Additionally if i could make appear an Arrow Button (Back) on ActionBar depending of the fragment would be great.

Thanks for your time! Regards.

How to&Answers:

Use this method in activity to change the Fragment and set the title programmatically:

private void displayFragment(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    String title = "";
    switch (position) {
    case 0:
        fragment = new Home();
        title = "Home";
        break;
    case 1:
        fragment = new Login();
        title = "Login";
        break;
    case 2:
        fragment = new RestorePass();
        title = "Restore Password";
        break;

    default:
        break;
    }

    // update selected fragment and title
    if (fragment != null) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_container, fragment).commit();
        getSupportActionBar().setTitle(title);
        // change icon to arrow drawable
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow);
    }
}

For example, you want Fragment Home to be displayed:

displayFragment(0);

Answer:

Keep in mind that if you are using the support Library, you have to specifically cast your Activity when you get it through getActivity(). And then you’ll want to make sure you are retrieving a support ActionBar by using getSupportActionBar(). I was able to set the ActionBar title in my app by using the following code in my Fragment’s onResume()

@Override
public void onResume() {
    super.onResume();
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    ActionBar actionBar = activity.getSupportActionBar();
    actionBar.setTitle(R.string.my_fragment_title);
}

Answer:

If you set the activity attribute android:label="" in the AndroidManifest you will be able to set the title post initialisation. I discovered this tracing through the Toolbar source code.