Possible way of hiding menuItem base on the screen(Activity) is in the foreground. I have four (4) menuItem and I want to show two (2) in the action bar and force 2 into the overflow menu, in some screen I want to show three (3) and have one (1) in the overflow menu,and in some screen the page tile is long I do not want it truncated but instead I want to show only one (1) menuItem in the action bar and force the other three (3) into the overflow menu.
I need a generic way to do this.
I need to do this programmatically I don't need the xml answer of IfRoom, I have a BaseActivity which extends ActionBar and I have BaseActivityHelper where I have my menu layout inflated, all my other activity extends the BaseActivity.
I would love to share my code but I'm not allowed to do so, the big question is if you are the one faced with this situation how will you do it.
The docs say:
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)
So, you can grab your menu item in onPrepareOptionsMenu and call it's setShowAsAction(int actionEnum) with the appropriate option (SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER)
Related
I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I'm trying to call it like this:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
Here's the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. onPrepareOptionsMenu gets called every time the menu needs to be displayed (instead of once, during setup).
So you can set a boolean or whatever to say whether the item should be shown, and call invalidateOptionsMenu() to redisplay it. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item
Under what condition you want it to be hidden, you can add it under that condition.
menu.visibility = View.INVISIBLE
or
menu.visibility = View.GONE
So I have 3 different buttons on my layout. Now as I was creating my layout for phones etc. I decided that having 3 buttons on that page was a waste of space. Those buttons already have a ton of code logic behind them, to set their visibility, replace strings depending on situation etc.
My question is, is there a simple way to change them to a menu, is it possible to simply copy the layout XML I had for them on the main page, and paste it inside a menu?
That menu would open from a simple button, and then present all 3 buttons inside of the menu. So they aren't taking up space the entire time. Will this break the code I already have? Imagine I have the following code. Will it still
bLogin.setText(getResources().getString(R.string.Exit));
bLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
And so on. I'm concerned with the menus behavior, will it close when I click one of the options, will the visibility attributes mess up the men, etc.
You could add a fourth button (let's call it menuButton) that is used to show/hide the other three buttons. You can then add the OnClickListener to the menuButton and set the visibility of the other three buttons from VISIBLE to GONE and the other way around.
So:
Button menuButton = (Button)findViewById(R.id.menuButton);
menuButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (buttonsVisible) {
otherButton1.setVisibility(View.GONE);
...
} else {
...
}
}
});
You could use menu or action bar, which would simplify things for you (you wouldn't have to worry about showing/hiding your buttons, etc). Check the docs. Also, if you're on Android 3.0+, it is recommended to migrate to action bar:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Of course, some work is required, but in the end, you will have a much more flexible solution (eg. if you will later want to add another action...)
My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.
Am designing an application in java using netbeans, in which i need to call a "Save" function by clicking on the save item on the menu bar. Am able to call the function by having a button inside the main panel, but am not able to call the function when i click on the menu bar or any where else. How can this be done?
Start with How to Use Menus, then see this example of How to Use Actions.
First check if you have defined an actionPerformed() method for the menuitem from which you are trying to call the method. If not, then you can define an actionPerformed() method by right clicking on the menu item in the design frame and select "events>actionperformed" from the context menu. Also take care that the menu items objects should not be a static field.
Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.
It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.
However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.
You probably want to create a custom ListPreference. Basically you want to extend from ListPreference (see original here), and provide a custom protected void onPrepareDialogBuilder(Builder builder), in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.
Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.
Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.