Navigation drawer title according to the Drawer item - android

I have used getSupportActionBar().setTitle("drawer_title"); and the title changes according to drawer item. But when I go back the title does not get updated. for eg: If I'm at dashboard-fragment toolbar-title is dashboard and after that when I go to message-fragment toolbar-title changes to message, but, after when I press back button it goes back to dashboard-fragment but the title does not updated to dashboard it remains message.
How could I update the title again??

In your fragment use requireActivity() to get the activity to the activitiy's SupportActionBar.
So, to set the title in the fragment then use:
((AppCompatActivity) requireActivity()).getSupportActionBar().setTitle("");

Add following line in onViewCreated method of your fragment:
getSupportActionBar().setTitle("Your title here");

Use getSupportActionBar().setTitle("drawer_title"); inside onResume() method of your fragment.

This works for me I just used getActivity().setTitle("fragment_title"); inside onCreateView() method of fragment.

Related

Hiding of a ToolBar icon only for a specific Fragment

I am using an Activity which has 3 fragment.Back Arrow is visible because of using the following line.
android:parentActivityName=".Activity.MainActivity". But I need to hide this <--(Back Arrow) only for a particular fragment abd it has to be visible in another 2 fragment. What do I need to do?
Use this line of code in your fragment onCreateView() where you want to remove the back button
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
or
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
try this code in your fragment:
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);

Changing Action Bar items and title on adding Fragments

Let's say I have a FragmentA visible.
I add a FragmentB (keyword: add, not replace), add it to the Fragment back stack and commit.
The issues I'm having doing this is:
1) The action buttons of the action menu of FragmentB are added, but those of FragmentA are not removed.
2) The title of the ActionBar does not change (despite calling getActivity().setTitle("FragmentB") in onResume() of FragmentB.
I can resolve both of these by calling replace instead of add when showing FragmentB however, for quite a few reasons I specifically need to add the Fragment instead (one of them being, I need to retain the state of FragmentA while showing B).
So how would I go about updating the ActionBar correctly as described?
Try this piece of code:
getActivity().getActionBar().setTitle("FragmentB");
Use this code in your Activity.. (for setting title to your fragment).
public void setActionBarTitle(String title) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
}
from your fragment onResume(), call this using
// Set title bar
((MyActivity) getActivity())
.setActionBarTitle("Fragment A");
and in each fragment you need to override onCreateOptionsMenu() to load your menus of that fragment.

Android: How to completely remove ActionBar

How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.

Proper way to configure ActionBar when using Fragments

I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.
I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

How can I refresh the fragment when an button is clicked in an item in android side menu

I have a checkbox for one of the items in my navigation drawer. On checking it I am setting a preference value to true. In one of my fragments I have a checkbox which is set based on this preference value. Everything works fine except for one case.
That is, when I am on this particular fragment. I open the navigation drawer and check/uncheck this checkbox. Close the drawer. The setting of checkbox is not affecting in my fragment. This is because fragment is not refreshing.
How can I do this?Anyone please help me out. Thanks in advance.
put the code inside onResume section of activity and call onresume() wherever you want.

Categories

Resources