I have a Option Menu that opens when I'm clicking the menu button on a android smartphone. And I wonder if there is some way to call the onCreateOptionsMenu and onOptionsItemSelected method from a onClick method for a button. So when I click on a button in my app instead of clicking the menu button on the smartphone.
You can invoke Activity.openOptionsMenu to show the options menu.
You should not invoke onCreateOptionsMenu(…) directly; this method is for the Android UI framework, not for you. If you need this method to be called again, for example, to refresh the menu items, you can call invalidateOptionsMenu() and Android will re-initialize the menu.
The same goes for onOptionsItemSelected(MenuItem). If you need to make the app do something that normally happens when a particular menu item is selected, simply put that something in a separate method that you invoke from onOptionsItemSelected as well as from anywhere else you need it.
Related
I am currently building an app with admin and user. I've used the template bottom navigation menu method. I've created two versions of the menu for admin and user. How will I be able to switch these two out at any point?
to switch between the two versions the bottom navigation menu, you can use the setOnNavigationItemSelectedListener method in the navigation view. This method allows you to set a listener that will be triggered when the user selects an item from the bottom navigation menu.
You can then use an if-else statement to determine if the user is an admin or a user and switch out the menus accordingly. You can also use the getMenu method to get the menu items and then use the setVisible method to hide or show certain menu items.
I am working with fragments and a navigation drawer. I have two fragments and a fragment manager to recycle them when I click on the menu items in the navigation drawer. I am also receiving data from a server in one of the fragments.
However, every time I reinitialize the fragment, the call to the server occurs again. I would like to know if there is any way that I can make sure that the server call only occurs in the beginning and when I click the refresh button.
I would like to know if there is any way that I can make sure that the
server call only occurs in the beginning and when I click the refresh
button.
Yes , when you click on the refresh button use an ClickListener to trigger the call.
If you want the call to occur only when fragment is created you can refer to the fragment life cycle. The better way is to put this call in the onCreate() method.
However if you don't want to make a call each time the fragment is created i recommend you tu use the SharedPreference to save the information that call have already been made.
exemple:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
// The seconde value of method getBoolean incates the default value if constant not found
if (prefs.getBoolean(Constants.CALL_DONE, false)) {
// Make call
prefs.edit().putBoolean(Constants.CALL_DONE, true).apply();
}
By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.
In a fragment I see in the onCreate setHasOptionsMenu(true);
I see the menu.
But I can't understand, if I need to change something in the menu where should I do that programmatically?
Now I see a menu which I am not sure which resource file is loaded but how can I change/update the menu before it is actually rendered e.g. to remove an item?
Update: I don't need todo this from the fragment. The hosting activity is fine but I don't know where should I do this and how
You can override onPrepareOptionsMenu(), which is called before the menu is displayed and everytime you call FragmentActivity.supportInvalidateOptionsMenu(). There you can call setVisible() to hide or show menu items as needed.
I need to change something in the menu where should I do that programmatically?
While menu is not managed by your fragment, I'd suggest to make fragment calling host Activity's method of your choice to request the change, but the change itself should be done by Activity
RoomInfoActivity.java: http://pastebin.com/L9fFsFeH
(note: this is the 3rd activity launched by the same application, not that it should matter..)
AndroidManifest.xml: http://pastebin.com/QbvQaTf3
room_info.xml: http://pastebin.com/DFNABSNF
Image: http://dl.dropbox.com/u/16952797/temp_stuff/elfapp_ss04.PNG
(note: when I click on the button, "nothing" happens.)
Description: So what happens is the code compiles just fine, and the .apk is launched and run without any issues, but the RoomInfoActivity doesn't reflect the changes that are supposed to be made (such as changing the text of the TextViews and Button) when I click on the button. I'm looking for the least complicated way to do these.
EDIT: I have now made a change, I added this line buttonCleaned.setClickable(true);
under this:
rumInfo.setText(getIntent().getExtras().getString("entry"));
buttonCleaned.setText("Färdig med städningen");
rumStatus.setText("Status: "+checkStatus());
Is your onClick() method ever called? I think you need to set the Activity as a click listener on the button, using the setOnClickListener method.