public class Profile extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
}
#Override //my code for action bar but it makes the action bar missing
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Have you tried explicitly returning true?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
Related
I am trying to hide/show menuItem but getting NPE. It works in onPrepareOptionsMenu but not in onCreate. By default it should be invisible.
e.g. in my fragment's onCreate i would like to do something like:
if (condition) menuItem.setvisible(true);
but it gets me NPE on that menuItem.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.inventory_list_toolbar, menu);
return true;
}
#Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_inventory);
mToolbar = findViewById(R.id.inventory_list_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
menuItem = menu.findItem(R.id.inventory_list_menu_add);
menuItem.setVisible(false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You can't hide menu from onCreate method since it's not inflated yet.
You should make your logic in onPrepareOptionsMenu method:
#Override
public void onPrepareOptionsMenu(Menu menu) {
if (condition) {
// do what you want
MenuItem menuItem = menu.findItem(R.id.menu_item_id);
menuItem.setvisible(true);
}
return super.onPrepareOptionsMenu(menu);
}
Otherwise, put your logic in onCreateOptionsMenu method and call invalidateOptionsMenu() when you need to refresh something.
private Menu menu;
Then initialize it as
#Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When u want to hide/show the menu use
if(menu != null){
MenuItem item_up = menu.findItem(R.id.action_up);
item_up.setVisible(true);
}
I want to hide some menu items when the search view expands so this is the code I used:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_refresh).setVisible(!isSearchOpen)
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search)
SearchView searchView = searchMenuItem.getMenu();
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
isSearchOpen = true;
invalidateOptionsMenu();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
isSearchOpen = false;
invalidateOptionsMenu();
return true;
}
});
}
Now this code works as expected except for one issue: when I click the search button, the SearchView does not appear. I cannot figure out what is the problem. I searched for similar issues on stack overflow but I didn't found any answer the fixed the issue, so any help will be welcomed
You can try this way to hide and show other options menu:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
final MenuItem delMenu = menu.findItem(R.id.action_delete);
final MenuItem editMenu = menu.findItem(R.id.action_edit);
MenuItem searchMenu = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenu.getActionView();
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
delMenu.setVisible(false);
editMenu.setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
delMenu.setVisible(true);
editMenu.setVisible(true);
return false;
}
});
return true;
}
I have followed some steps on hiding and showing menu items. But unfortunately my app crashes. What could I be doing wrong?
This is my Activity:
private MenuItem menuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
invalidateOptionsMenu();
menuItem.setVisible(false);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
return super.onCreateOptionsMenu(menu);
}
Thanks in advance for any help! :D
Update
private MenuItem menuItem;
private boolean menuItemShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol_type_page);
}
//TOOLBAR SETTINGS
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu_icon, menu);
menuItem = menu.findItem(R.id.view_orders);
menuItem.setVisible(menuItemShow);
return super.onCreateOptionsMenu(menu);
}
onCreateOptionsMenu is invoked after onCreate,you shouldn't set menu item visibility in onCreate method.
Ok so I have multiple buttons on the home page of my android application.
I have seem to have got confused along the way as I have coded the app like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void aboutus (final View view) {
setContentView(R.layout.aboutus);
System.out.println("about us clicked");
}
And I have put the on click in the xml.
However I have things that I need to write code for in the "about us" page.
In my about us class I have the following
public class AboutUs extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
}
Here is the xml code for the button that is being clicked
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="134dp"
android:layout_height="97dp"
android:layout_x="16dp"
android:layout_y="150dp"
android:onClick="aboutus"
android:src="#drawable/aboutus" />
Any code I am writing isn't working on that page? I don't know why? can any one help?
Thank you.
Change your MainActivity like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void aboutus(final View view)
{
startActivity(new Intent(MainActivity.this, AboutUs.class));
}
and AboutUs.java is like:
public class AboutUs extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/* No need of this method */
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
}
NOTE:
android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.
You can't just call setContentView in your aboutus method, you need to create an intent that starts a new activity. Calling setContentView just sets the view in the current main activity, and doesn't instantiate your AboutUs activity class. Your aboutus method should look similar to:
public void aboutus (final View view)
{
Intent intent = new Intent(this, AboutUs.class);
startActivity(intent);
}
I think you are using setContentView() to call another Activity.
You must use Intents
Intent in = new Intent(this,AboutUs.class);
startActivity(in);
I have a listview and call detail activity when the item is selected.
My onCreateOptionsMenu has error on displaying the menu at Action Bar.
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
The error is The method onCreateOptionsMenu(Menu, MenuInflater) in the type Fragment is not applicable for the arguments (Menu). Error happened at return line.
I implement listview and detail activity using fragmentTransaction.
Thanks
Your onCreateOptionsMenu(Menu menu) just needs to be in the activity that hosts the fragment, not in the fragment itself.
You could also consider extending a BaseActivity and including it in there just once.
public class BaseActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getActivity()).inflate(R.menu.detail_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
}
public class ListActivity extends BaseActivity {
// ...
}
public class DetailActivity extends BaseActivity {
// ...
}
Try it like this but put it in your main Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.detail_view_menu, menu);
return true;
}
OR if you want your Fragment to add items to the ActionBar you have to use a slightly different construct:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
You have an additional parameter that you have to add(MenuInflater). Also, in a Fragment the onCreateOptionsMenu doesn't return a boolean value.
Now that you have your inflater you need to call setHasOptionsMenu(true) in your Fragment's onCreate() method. Otherwise your items will not be displayed in the ActionBar.
Your Fragment code, handling the menu inflation, should now look like this:
public class DetailFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.detail_view_menu, menu);
}
}