I am using navigation drawer in my application, with fragments. There are icons and text in each row(one of them is products, on clicking these a fragment is open with a list view.After clicking on the list view row i have an activity in this i want header name same as i have selected(products).but bydefault it is showing name off the app which is sales.please help.
pass product name in Intent in your list's onItemClickListner
Intent intent = new Intent(getActivity(),YOURACTIVITY.class);
intent.putExtra("product_name",YOUR PRODUCT_NAME);
getActivity().startActivity(intent);
and in your target activity (your product detail activity) put below code in onCreate() method.
getSupportActionBar().setTitle("YOUR DESIRED TITLE");
If using Frament , you can have a method in parent hosting Activity like
this
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
and invoke this method from your individual fragment like this and set the title
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//usual statements
((HomeActivity)getActivity()).setActionBarTitle("Child fragment name");
}
You can set the actionbar header with the following. You can try this.
ActionBar actionBar = getActionBar();
actionBar.setTitle("My Title");
Related
In my activity, I have 5 different fragments. I want to change the title for only one Fragment other Fragment's title will be same. I've used following code, but it changes title for all Fragments. How can I change title for only a specific Fragment?
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Create Post")
I solved this problem by creating class named StatefulFragment (extends Fragment) and creating String fragmentTitle inside this class which contains your title (like this)
public class StatefulFragment extends Fragment {
public String fragmentTitle = "";
}
Use this class for initialize all your fragments (extend them from StatefulFragment).
Then I recommend to hide ActionBar from your activity and add Toolbar to your all fragment's views (and initialize it in fragment's class). And then just set fragment's title which we added to fragment's toolbar:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState){
Toolbar myToolbar = view.findViewById(R.id.myToolbar);
myToolbar.setTitle(fragmentTitle);
super.onViewCreated(view, savedInstanceState);
}
Hope it helps!
I have an application with two fragments when the application starts the Actionbar title is the one of the first fragment. But when I'm going to the second Fragment the title doesn't change.
So how do I change the ActionBar title?
This is my code:
public class ClientList extends Fragment {
#Override
public void onResume() {
super.onResume();
String title;
title = getResources().getString(R.string.client_title);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title);
}
#Override
public void onDestroyView() {
super.onDestroyView();
String title2;
title2 = getResources().getString(R.string.action_settings);
ActionBarActivity action = ((EmployeeActivity)getActivity());
action.getSupportActionBar().setTitle(title2);
}
}
so if you say you cannot reach the method from onCreateView, so this is your solution I think:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment,
container, false);
getActivity().setTitle("Title");
return view;
}
Because if you return before you call setTitle() you cannot reach it, that's true.
Hope it helps.
You can set your action bar title inside onCreate(if activity)/onCreateView(if fragment) method as getSupportActionBar().setTitle("Your Title");
Whenever you switch from one fragment to another fragment you can set the title in ActionBar. This can be done on onCreateView(...) of a fragment. Advisable try to change the name in Activity in which you are trying to load the fragment.
To do so ....
If you are using appcompat library
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(" your title ");
If you are not using appcompat library
ActionBar actionBar = getActionBar();
actionBar.setTitle(" your title ");
This should work:
getActivity().setTitle(YOUR_TITLE);
Call it from your Fragment when you wanted to set.
i want to use custom actionbar with fragment title.but this method only display application name for all fragments.i need to use appropriate fragment name in actionbar.
getActivity().getActionBar().setDisplayShowCustomEnabled(true);
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(getActivity());
View v = inflator.inflate(R.layout.title, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(getActivity().getTitle());
//assign the view to the actionbar
getActivity().getActionBar().setCustomView(v);
You just need to define a method which will update the title of action bar and call that method from ever activity / Fragment's onResume()
In activity :
#Override
public void onResume() {
// For activity
setActionbarTitle("title");
// For Fragment
((ParentActivtyNAme)getActivty).setActionbarTitle("title");
}
setActionbarTitle Method :
public void setActionbarTitle(String title) {
textTitle.setText(title);
}
And initialize textTitle when you set Custom View in Actionbar :
textTitle= ((TextView)v.findViewById(R.id.title));
Hope it helps you ツ
Problem:
The Fragment backstack is made so that traversing backwards through a stack of fragments in one activity does not revert the Action Bar to its original state in the previous fragment.
Why does this happen?
Turns out, the Action Bar is actually attached to the Activity itself, not the fragment! Remember, fragments are only modular bits of the UI, and have to explicitly specify control to other fragments, sections of the activity, or even the Action Bar.
Keep reading for the solution...
Solution:
I found that the best approach to this problem is done by what is generally described in Reto Meier's answer to a previous question. My solution will just expand more deeply on his answer.
What we want to establish though is that we don't want to re-create the action bar every time we switch to a different fragment, reason being it's not very efficient. I'm going to walk you through an I wrote for a student scheduling app. It's not very complicated, and it's onboarding experience is composed of multiple fragments held within an activity.
To make this work, we need to make sure we're using replace() to switch between fragments. This is better than layering fragments on top of each other, because it lets you configure the action bar separately for each fragment.
The first chunk of code comes from the activity's inner class, LoginOptionsFragment, in its onCreateView() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login_options, container, false);
//LoginOptionsFragment will have its own action bar
setHasOptionsMenu(true);
//inject views. e.g: Button add_course
ButterKnife.inject(this, rootView);
add_course.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
//exchange fragments. no messy clean-up necessary.
.replace(R.id.container, new AddCourseFragment())
.addToBackStack(null)
.commit();
}
});
return rootView;
}
Here, I not only make sure to call onCreateOptionsMenu() via the setHasOptionsMenu(true), but mainly, as soon as the "ADD COURSE" button is clicked to switch to the AddCourseFragment, the new fragment replaces the old fragment as the primary child of the activity. Next, after we override the onCreateOptionsMenu(), we come to onResume(), but we'll get to that later ;)
Secondly, we arrive at the AddCourseFragment, where we even inflate a custom done-cancel view for the action bar. So let's look at the code!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done/Cancel" custom action bar view.
final ActionBar actionBar = getActivity().getActionBar();
inflater = (LayoutInflater) actionBar.getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
//inflate custom action bar view
View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_cancel, null);
//set listeners to items in the view
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
// "Done"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
//add course to list
}
});
customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
// "Cancel"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayHomeAsUpEnabled(false);
// END_INCLUDE (inflate_set_custom_view)
View rootView = inflater.inflate(R.layout.fragment_add_course, container, false);
ButterKnife.inject(this, rootView);
return rootView;
}
The ONLY part that you need to pay attention to are the OnClickListener's added to the DONE and CANCEL buttons. In here, I use my previous reference to the parent Activity's action bar and tell it to stop displaying the custom view. Now in addition to this specific method, there are more setDisplayXEnabled() methods that you can pass in false to. After that, I pop the backstack to get to the previous fragment.
But how do I actually revert the action bar!?
Here's how. Remember that onResume() method that was hanging out in our LoginOptionsFragment? Well, onResume() is called once a fragment gets back into focus from the backstack! So if we override it and re-enable the parts of the action bar that we want, we win right? Yes we do. Here is all you need to add into the onResume().
#Override
public void onResume() {
super.onResume();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true); //show Home icon
actionBar.setDisplayShowTitleEnabled(true); //show title
// actionBar.setDisplayUseLogoEnabled(true); <--- more options
// actionBar.setDisplayHomeAsUpEnabled(true); <--- more options
}
And we did it all without recreating the action bar. Here's how it looks!
Thanks for reading, and happy coding!
I've downloaded the sample project from the Android official site http://developer.android.com/training/implementing-navigation/nav-drawer.html. I am trying to understand how the Navigation Drawer works. So, I have one doubt, they call a Fragment for each item from the left menu. In my project, I have a big activity which I am trying to call by this Fragment:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = new Intent();
intent.setClass(getActivity(), ListMatch.class);
startActivity(intent);
View rootView = inflater.inflate(R.layout.list_match, container, false);
return rootView;
}
}
But, if I do that, it calls perfectly my Activity, but the menu disappear. How can I call this activity and keep my menu? Thanks a lot.
Like Raghunandan said, the drawer applies wihin a single activity. It's common to launch a new activity from the "main" drawer, but usually that kind of activity would have the up action set in the action bar to go back to the main activity.