How can I change option menu in different fragments? - java

I have a Fragment with menu:
public class FragmentA extends Fragment {
public FragmentA() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_main_actions, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
I would like to change menu but it doesn't work and keep the old action menu
Fragment B is equals like above with different inflate XML menu.
public class FragmentB extends Fragment {
public FragmentB() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.**action_ranking**, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
EDITED:
Can be useful to use different menu layout for different fragments and 1 menu layout for main activity and differents id

Put setHasOptionsMenu(true) in constructor and inflate fragment specific menu.
public class FragmentA extends Fragment {
public FragmentA() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragmenta_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
menu in main activity
public class MainActivity extends Activity {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}

Can all be done via Fragment - no need to inflate menu from activity:
public class UpdateFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.update_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.navUpdateProfile:
showToast("navUpdateProfile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

If you have several fragments that share the same menu with some exceptions.
class BaseFragment:Fragment(){
open var menuId = R.menu.default_menu
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true) // will apply to all children
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(menuId, menu) // will apply to all children except for overridden
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// all menu ids can be listed here unless specific to code in child
when (item.itemId) {
R.id.menu_option_1 -> {
// do something
}
R.id.menu_option_2 -> {
//do something
}
return false
}
}
class ChildFragment:BasFragment(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
menuId = R.menu.menu_2 // change to a different menu as desired here
}
}

Related

How do I show menu again on onPrepareOptionsMenu( );

Please help,
I want the menu to be hidden when I enter the Activity at the begining.
And after progressbar is set to gone, the menu will not be hidden.
But when I ran my app, the menu was always hidden.
I don't know how to show the menu on screen again.
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
private ActivityHomeBinding binding;
private ActionBar actionBar;
private Menu menu;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityHomeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
actionBar = getSupportActionBar();
progressBar = binding.progressBar;
progressBar.setVisibility(View.VISIBLE);
// it's not work
onPrepareOptionsMenu(menu);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.function_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (progressBar.getVisibility() == View.VISIBLE) {
onCreateOptionsMenu(menu);
return false;
} else {
onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.function_menu, menu);
return true;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_home:
intent = new Intent(HomeActivity.this, HomeActivity.class);
homeActivityResultLauncher.launch(intent);
return true;
case R.id.menu_summary:
intent = new Intent(HomeActivity.this, SummaryActivity.class);
summaryActivityResultLauncher.launch(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

How to programmatically hide menu on action bars in Android?

This is how I declare my menu on my action bars:
public void checkUserType() {
if (mPrefs.getUserType().equalsIgnoreCase("mahasiswa")) {
requestData(String.valueOf(mPrefs.getUserID()));
} else if (mPrefs.getUserType().equalsIgnoreCase("dosen")) {
requestData(String.valueOf(mPrefs.getSelectedUserId()));
getSupportActionBar().hide();
myMenu.findItem(R.id.exit).setVisible(false);
}
And I want to hide/remove the menu from the action bar using an if-else (not hiding whole action bar, just the menu). I have tried using "myMenu.findItem(R.id.exit).setVisible(false);" but it occurs error on a null object reference
This is my code:
public void checkUserType() {
if (mPrefs.getUserType().equalsIgnoreCase("mahasiswa")) {
requestData(String.valueOf(mPrefs.getUserID()));
} else if (mPrefs.getUserType().equalsIgnoreCase("dosen")) {
requestData(String.valueOf(mPrefs.getSelectedUserId()));
getSupportActionBar().hide();
myMenu.findItem(R.id.exit).setVisible(false);
}
This will hide a menu item by id:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate( R.menu.main_menu, menu );
// hide menu item
menu.findItem( R.id.menu_item_1 ).setVisible( false );
return true;
}
You can apply the same to:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if( item.getItemId() == R.id.menu_item_1 ) {
item.setVisible( false );
}
}
here just call invalidateOptionsMenu() and move your logic to onCreateOptionsMenu() and change visiblity there.
for example
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
invalidateOptionsMenu();
}
});
}
and in onCreateOptionsMenu()
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
menu.findItem(R.id.menu_item).setVisible(condition);
}

onCreateOptionsMenu is not called after a fragment add & remove

I have a fragment where I can call ActivityCompat.invalidateOptionsMenu(getActivity()); and the method onCreateOptionsMenu() is successfully called on both: Activity and Fragment.
However, when I add a fragment on top of the fragment with:
transaction.add(R.id.fragment_home, fragment2, "fragment_ID");
transaction.addToBackStack(null);
transaction.commit();
and later close it (either by backPress or by getSupportFragmentManager().popBackStack();), ActivityCompat.invalidateOptionsMenu(getActivity()); becomes unresponsive. onCreateOptionsMenu() is not called at all.
I know for sure, that it will work if I use replace() instead of add(), however, for some reasons, I need to use add().
on Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
if (fragment != null)
fragment.onCreateOptionsMenu(menu, getMenuInflater());
return true;
}
on Fragment 1:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu() called with: ...");
// additional code
super.onCreateOptionsMenu(menu, inflater);
}
Please try below code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
if (fragment != null) {
fragment.onCreateOptionsMenu(menu, getMenuInflater());
return true;
}
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}

Hide 3-dot menu item when the Navigationbar appears

I want to hide the three-dot menu item when the navigationbar appears.
I found some nice topics how to hide the three-dot menu item:
How to disable/hide three-dot indicator(Option menu indicator) on ICS handsets
How do I hide a menu item in the actionbar?
But I can not find a solution for my problem. I hope for some help. :)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
infoDialogFragment.aboutMenuItem(this);
return false;
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
private void setNavigationDrawer() {
navigationView.setNavigationItemSelectedListener(menuItem -> {
});
return false;
}
Delete all of this codes from your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
infoDialogFragment.aboutMenuItem(this);
return false;
}

Android onOptionsItemSelected method of Fragment doesn't get called

there are many questions about the topic but i can't figure out my problem. I have a menu declared in my MainActivity(ActionBarActivity). Now i want to work with MenuItem in onOptionsItemSelected of a Fragment class. Here is my MainActivity methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.add_note:
createNewNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNewNote() {
Intent addIntent = new Intent(MainActivity.this, AddNote.class);
startActivity(addIntent);
}
And Fragment methods
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
Toast.makeText(getActivity(), "Entered into fragment", Toast.LENGTH_LONG).show();
createNewNote();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void createNewNote() {
Intent addIntent = new Intent(getActivity(), AddNote.class);
startActivity(addIntent);
}
Here in MainActivity onOptionsItemSelected get called even in Fragment but doesn't in Fragment as i don't see Toast in Fragment. I think something is missed in my code. Thanks in advance.
inside onOptionsItemSelected() in activity inside your switch after calling createNewNote() instead of returning true return super.onOptionsItemSelected(item)
you must call setHasOptionsMenu() inside onCreate of fragment for menu related method to work.
Your onCreateOptionsMenu() method does not inflate the menu file:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}

Categories

Resources