When I'm opening the overflow menu, at first I see just an empty layout, and only after that the menu with text appears:
The same thing happens when the overflow menu is closing:
Creating this menu seems to be standard:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cp:
return true;
case R.id.action_settings:
return true;
case R.id.action_used_libraries:
return true;
case R.id.action_help_and_feedback:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
why does it happen and how to get rid of this lag?
Try removing <item name="android:background">#color/yourgreencolor</item> from both your ThemeOverlay.AppTheme.ActionBar and ThemeOverlay.AppTheme.PopupMenu style, it's rendering a background first. I used parent="ThemeOverlay.AppCompat.Light" to have my desired white background, maybe that could help you find out how to have a green background despite this issue. It's definitively possible to have custom colors without this, I've seen some.
Related
i have a note taking activity where i would like to be able to edit and save (basically overwrite) the same note.
Once i click the edit MenuItem, I would like it to hide and then show the save MenuItem.
I can get the edit MenuItem to be invisible but i cant get the save MenuItem to show. i keep getting a null pointer exception.
here's my edit_question_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/edit_question"
android:icon="#drawable/ic_edit_black_24dp"
android:title="Edit"
app:showAsAction="ifRoom"
android:visible="true">
</item>
<item
android:id="#+id/save_question"
android:icon="#drawable/ic_save_black_24dp"
android:title="Edit"
app:showAsAction="ifRoom"
android:visible="false">
</item>
</menu>
activity.java file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.edit_question_menu, menu);
MenuItem itemSave = menu.findItem(R.id.save_question);
itemSave.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_question:
saveQuestion();
return true;
case R.id.edit_question:
item.setVisible(false);
// MenuItem save_Question_MenuItem = findViewById(R.id.save_question);
// save_Question_MenuItem.setVisible(true);
enableEditMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void enableEditMode(){
MenuItem saveButton = findViewById(R.id.save_question);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black_24dp);
questionEditText = findViewById(R.id.questionEditTextID);
mPostAnswerButton.setEnabled(false);
mPostAnswerButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
mCommentButton.setEnabled(false);
mCommentButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
saveButton.setVisible(true); **ERROR HAPPENS HERE**
}
Any and all help is appreciated. This seems fairly simple but I cant find a way to get it to work.
Add flag to your activity that indicates where the save MenuItem should be visible or not:
private boolean mShowSaveIcon
Override onPrepareOptionsMenu (UPDATE):
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.save_question);
item.setVisible(mShowSaveIcon);
menu.findItem(R.id.edit_question).setVisible(!mShowSaveIcon); // you can use negation of the same flag if one and only one of two menu items is visible; or create more complex logic
return true;
}
In the click handler, change flag value and request invalidation:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_question:
mShowSaveIcon = false;
break;
case R.id.edit_question:
item.setVisible(false);
enableEditMode();
mShowSaveIcon = true;
break;
}
invalidateOptionsMenu();
return true;
}
Change handler:
private void enableEditMode(){
/// MenuItem saveButton = findViewById(R.id.save_question); // <--- NO THIS LINE
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black_24dp);
questionEditText = findViewById(R.id.questionEditTextID);
mPostAnswerButton.setEnabled(false);
mPostAnswerButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
mCommentButton.setEnabled(false);
mCommentButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
/// saveButton.setVisible(true); **ERROR HAPPENS HERE** // <--- NO THIS LINE
}
For some reason, the background tiles for the options are not showing when I click the overflow button at the bottom of the screen.
I have gone over the code, but I can't find anything that would cause such a thing. There is basically no background behind the text. If I change the background colour to black then the text won't be seen.
Here is all the code related to the menu options:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_game:
gameView.startNewGame();
return true;
case R.id.stats:
return true;
case R.id.help:
gameInfo.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return (super.onCreateOptionsMenu(menu));
}
Also according to this post: https://stackoverflow.com/a/13689548/1973276, the way to get the overflow button to show at the bottom is to set the tarketSDKVersion to 13. That is what I did to get it to appear at the bottom of the screen, but since that reply was 2 years ago, SDK 13 has long be replaced and it is recommended to use the latest SDK. Is there a better way in placing the overflow button in the bottom bar?
Any help would be appreciated.
I have a samsung galaxy tab3 and I have the menu implemented in the usual manner - i.e.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
and for menu selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In my xml ive used android:showAsAction="never" for items that I want to see in overflow and android:showAsAction="ifRoom" for items I want to see upfront. But in this tablet I see only the ones with ifRoom set and the others just disappear and the menu button does not show the overflow items. I've looked at all answers on SO and tried the popular ones like setting minimum and target sdk versions to less than 11 and so on. But the menu just wont come up. It works fine in devices that dont have the capacitive menu touch and an overflow button is shown in the action bar.
Well it ended up working when I programmatically called openOptionsMenu with onKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
openOptionsMenu();
}
return true;
};
MAybe you have a theme (like full screen) that does not support an ActionBar?
Note: PopupMenu is available with API level 11 and higher.
http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
With that in mind, how can I link a menu to a View on screen with ABS and the compatibility library? I have a menu imageview on one of my layouts that I would like to provide a universal menu option. Basically by having an imageview that pops up the menu, the same system is in use regardless of device or android version. But if popup only exists on Gingerbread and later, is there another way to link an ImageView (+ click listener) to open up my menu?
Here's how I ended up solving this:
ImageView menuImg = (ImageView) activity.findViewById(R.id.menuImageView);
menuImg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.openOptionsMenu(); //This is the key method!
}
});
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.demographics:
return true;
case R.id.settings:
Log.v("v", "settings clicked");
return true;
default:
return false;
}
}
I have been running into some troubles recently and I think I need your help :).
I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data.
I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method.
My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens.
Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)
I would be glad to try any solution you could provide.
Thanks a lot
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==0){
getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else
if(l>1)
getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
else
dismiss();
return true;
}
Maybe this little snippet out of my app helps you:
private static final int REFRESH_ID = Menu.FIRST + 1;
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
return result;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case REFRESH_ID: {
// Do whatever you want here!
return true;
}
}
return super.onMenuItemSelected(featureId, item);
}
As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;)
And in the onMenuItemSelected you can switch those constants easily.
Hope that helps!