I have a Activity as below:
// My Activity Holding Fragment
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
}
}
// My Fragment
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
}
}
I wan to have the Title Bar for my Activity.
I want to Hide or Show Title Bar from Activity and Frgment on some conditions.
I tried below solutions, but as my activity does not derive from ActionBarActivity it does not work.
// getSupportActionBar returns null
getSupportActionBar().hide();
getSupportActionBar().show();
// getActionBar returns null
getActionBar().hide();
getActionBar().show();
// findViewById returns null
findViewById(android.R.id.title).setVisibility(View.GONE);
findViewById(android.R.id.title).setVisibility(View.VISIBLE);
// Are deprecated
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
None of the solutions did work for hiding Title Bar from Activity or Fragment.
Basically I want to hide the Activity title bar for the Activity.
First Change your App theme from res/value/styles.xml to this
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
//leave everything as it is
</style>
After that Add toolbar in your Activity's XML Layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/colorAccent"
app:titleTextColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//add your toolbar design here
</toolbar>
Now finally set up your toolbar in Activity Class
public class MyActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
//need to set toolbar before calling getSupportActionBar
setSupportActionBar(toolbar);
//Now show or hide according to your need
getSupportActionBar().hide();
getSupportActionBar().show();
}
}
Same would be applicable for fragment but for fragment you need to call view first
Toolbar toolbar = getView().findViewById(R.id.toolbar);
Related
What I am trying to achieve is to add a Toolbar to my Fragment UI when my Activity also has a bar that is NOT shown inside the fragment. Basically, how can my Activity and Fragment(s) have their own toolbar?
This is my code in my activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(mToolbar);
}
And here is my code in my fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_layout, container, false);
mToolbar = (Toolbar) mView.findViewById(R.id.fragment_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
return mView;
}
What I get is that when I create the toolbar in my Fragment, the toolbar is the toolbar from the activity. What I want to do is I want the fragment to have it's separate toolbar from it's parent Activity.
Here are a few things that I can think of which are definitely not ideal:
Make the fragment an activity.
Simulate as if I have created a toolbar but simply have the Fragment toolbar defined as a UI element that I have pieced together myself.
Somehow edit the parent Activity's toolbar to make it specific to the fragment, and then somehow redraw the Activity toolbar? This seems weird and I am not even sure if it's possible.
Having 2 toolbars in one activity is not advisable since it'll look really bad.
with ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
you're replacing the activities toolbar with the one that you're defined in the fragment's layout file.
I would suggest you to use fragment specific actions for the toolbar when the fragment is visible on the screen and change it's behavior by accessing the content of the toolbar by
Toolbar mToolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
now use the mToolbar to change the actions
I am having an optionMenu at my MainActivity inflated inside onCreateOptionsMenu of my MainActivity and there are some fragments which are inflated inside the MainActivity which have their own Toolbars. But the problem is that the OptionMenu is also visible on the Toolbars inside the fragments. I don't want the fragments to have the OptionMenu for Fragments toolbar
setHasOptionsMenu(true) in your fragment onCreate();
in onCreateOptionsMenu inflate your fragment menu and remove MainActivity by using menu.removeItem(R.id.menu_id_to_be_removed); in onPrepareOptionsMenu
For the fragment you don't want onCreateOptionsMenu include following code in those fragments
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
You can remove/hide overflow icon from toolbar by following the below steps
In your fragment where you want to hide it.
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_create_group,container,false);
setHasOptionsMenu(true);
}
override "onPrepareOptionsMenu". Like below
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
menu.clear();
}
I just googled for "setsupportactionbar fragment", but all I found was "getsupportactionbar from fragment". Is that similar to each other, or is there something special?
Yes it is similar, but you have to keep in mind that you have to place setSupportActionBar() in onCreateView if you want to use elements of fragments layout-file:
..
public class FragmentOne extends Fragment {
..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return view;
}
..
}
you should not use setSupportActionBar() in fragment. because every time you add this fragment on activity a new instance of toolbar is created. so every time a new Toolbar is showing in screen. so it is not a good approach.
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 made an abstract base activity called MyBaseActivity that extends Activity. I then extend MyBaseActivity for all of my concrete sub-activities. I did that so that I wouldn't have to set up the same menu for every single sub-activity.
However, I still have the following code repeated in all of my activities' onCreate() calls.
// Custom View for ActionBar
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View view = View.inflate(getApplicationContext(), R.layout.actionbar_top, null);
actionBar.setCustomView(view);
Is there a way to avoid repeating this code? Can I put it in MyBaseActivity? If so, do I need to send it the context? How would I do that?
Put it in your base Activity's onCreate().
In the subclass, when you call super.onCreate() the code will be executed.
public class BaseActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Custom View for ActionBar
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View view = View.inflate(getApplicationContext(), R.layout.actionbar_top, null);
actionBar.setCustomView(view);
}
}
public class SubActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // this calls BaseActivity's onCreate()
// at this point your actionbar custom view will have been set up
}
}