How to dismiss overflow menu when home button is pressed android? - android

I am displaying overflow menu list when we click the three dots. When I press home button and again when I launch the app, overflow menu list is still displaying. How to dismiss overflow menu list window?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v("RAMKUMARV ONCREATEOPTION", "RAMKUMARV");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_settings);
item.setVisible(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

You can have an activity field that stores the options/overflow menu whenever the onCreateOptionsMenu() gets triggered, and then use close() method to dismiss the menu when the home button is clicked i.e. in onUserLeaveHint().
public class MainActivity extends AppCompatActivity {
// Field to store the overflow menu
private Menu mOverflowMenu;
// omitted rest of your code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mOverflowMenu = menu;
// omitted rest of your code
return true;
}
// Dismissing the overflow menu on home button click
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
mOverflowMenu.close();
}
}

Declare the menu item like at MainActivity level, like this:
public class MainActivity extends AppCompatActivity {
private MenuItem overflowItem;
...
}
Now you should instanciate the object inside the onCreateOptionsMenu method, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
overflowItem = menu.findItem(R.id.action_settings);
overflowItem.setVisible(true);
return true;
}
Then you can use the onPause method to set the overflowItem not visible anymore:
#Override
public void onPause() {
super.onPause();
overflowItem.setVisible(false);
}
Finally remember to replace your MenuItem item object with overflowItem where it is used.

To dismiss the overflow menu you can simply call closeOptionsMenu() inside Activity.
closeOptionsMenu()
Progammatically closes the options menu. If the options menu is
already closed, this method does nothing.
You can call it in onPause() method like below:
#Override
public void onPause() {
super.onPause();
closeOptionsMenu();
}
or in case you want to call it only when the user presses the Home key you can use onUserLeaveHint() like below:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
closeOptionsMenu();
}

Related

How do you "deflate" menus in android?

I have a contextual action bar, where I have a setting called "DELETE". When I press that button I want the CAB menu to disappear.
class MyActionModeCallBack implements android.view.ActionMode.Callback {
#Override
public boolean onCreateActionMode(android.view.ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.event_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode actionMode, MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.delete) {
*******
_root.removeView(view);
// This is where I want to remove the CAB menu
*******
}
return false;
}
#Override
public void onDestroyActionMode(android.view.ActionMode actionMode) {
((RelativeLayout)view).removeAllViews();
view.setBackgroundColor(0xFF00FF00);
view.setTag(R.string.viewSelected, "0");
}
}
I was thinking about calling the onDestroyActionMode() right after the _root.removeView(view);, but I don't know what arguments to pass in.
Any suggestions are appreciated, thanks!
If you are trying to hide the item being selected, you can call
menuItem.setVisible(false);
Alternatively, if you want to remove all items from the menu, you can call
optionsMenu.clear();
in the onActionItemClicked() method.
You can save your menu to a field in your onCreateOptionsMenu method as follows:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
optionsMenu = menu
}
Call actionMode.finish(). This will invoke the onDestroyActionMode() callback.

Returning back to a previous activity?

I have an main activity that opens a child activity:
startActivity(new Intent(this, ChildActivity.class));
From within the child activity, I press the Back button to return back to the main activity. I also have this code in the main activity:
#Override
protected void onResume() {
super.onResume();
dosomething();
}
However, onResume is never reached.
Am I missing something?
This way, it's calling onResume() when you go back from child Activity to parent: Activity:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextView = (TextView)findViewById(R.id.mTextView);
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ChildActivity.class);
startActivity(intent);
}
});
}
#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);
return true;
}
#Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"Yes calling",Toast.LENGTH_LONG).show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ChildActivity.java
public class ChildActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar mtooToolbar= (Toolbar)findViewById(R.id.mtoolBar);
setSupportActionBar(mtooToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#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_child, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id==android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
onResume() is called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().
Check this link to understand the relationship:
Activity Lifecycle
you can use finish() method to finish your activity.

onprepareoptionsmenu was called by default

I got a need to change the menu items dynamically during a click event of menu items.
So I implemented onOptionsItemSelected in an activity.
public class ResultActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
}
and i'm calling invalidateOptionsMenu in the onOptionsItemSelected method which inturn should call the onPrepareOptionsMenu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_group: {
invalidateOptionsMenu();
break;
}
}
}
Also, I am trying to remove one item from menu in onPrepareOptionsMenu method.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG,"In onPrepareOptionsMenu");
menu.removeItem(R.id.action_group);
Log.d(TAG,"Group icon is removed");
return super.onPrepareOptionsMenu(menu);
}
The problem is, item is deleted during the launch of activity rather than wait until corresponding menu item is clicked.
Can someone tell what the problem is.. TIA
The problem is that onPrepareOptionsMenu(Menu) gets called anyway, anytime your menu needs to be shown or reloaded. That includes calls coming from invalidateOptionsMenu(), but also from the Activity being created.
You could, for example, check for a boolean state before actually removing the item.
public boolean mRemoveItem;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_group: {
mRemoveItem = true;
invalidateOptionsMenu();
break;
}
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mRemoveItem) {
menu.removeItem(R.id.action_group);
}
return super.onPrepareOptionsMenu(menu);
}
According to your needs, you will need to set mRemoveItem back to false at some point in your code.

Android - How to hide menu option for current fragment

I have an ActionBar activity with a FrameLayout and a menu. when the user clicks the menu item I replace the fragment with the relevant new fragment. However, I cannot see an obvious way to remove the menu item for the selected fragment.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
StudyFragment startFragment = new StudyFragment();
startFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add
(R.id.container, startFragment).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_study:
replaceFragment((Fragment)new StudyFragment());
break;
case R.id.action_list:
replaceFragment((Fragment)new ListFragment());
break;
// etc
}
return super.onOptionsItemSelected(item);
}
private void replaceFragment(Fragment f) {
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, f);
transaction.addToBackStack(null);
transaction.commit();
}
The Google documentation on changing menus says to disable the menu in onPrepareOptionsMenu - but how will I know which item has been selected?
--Solution Implemented--
Using Muhammed Refaat's solution below I added two new members to the class:
private Menu activityMenu;
private MenuItem curMenuItem;
Set them in onCreateOptionsMenu
activityMenu = menu;
curMenuItem = activityMenu.findItem(R.id.action_study);
curMenuItem.setVisible(false);
And changed them on onOptionsItemSelected
curMenuItem.setVisible(true);
curMenuItem = activityMenu.findItem(id);
curMenuItem.setVisible(false);
First get the item you want to remove :
MenuItem item = menu.findItem(R.id.your_action);
then set it's Visibility false :
item.setVisible(false);
and if the problem is in getting the menu (as it's not in the fragment), you can easily get a context from the activity that contains the menu and get the menu by it.
Inside your fragment you will have to use setHasOptionsMenu(true); in order to access options menu from within your fragment.
Code (inside your second fragment where you wanna hide the item):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO your code to hide item here
super.onCreateOptionsMenu(menu, inflater);
}
Similarly, for your fragment where you want to show that MenuItem you can do the similar thing.
In the fragment where you want to hide the Item
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item=menu.findItem(R.id.action_search);
item.setVisible(false);
and in onCreate() of your fragment
setHasOptionsMenu(true);
Adding to Muhammed's answer above. Once the item has been set as invisible, you may need to also disable the item. Note Google's comment: "Even if a menu item is not visible, it may still be invoked via its shortcut (to completely disable an item, set it to invisible and disabled)" under setVisible() in the MenuItem documentation.
Thus:
item.setVisible(false);
item.setEnabled (false);
Add below codes into your fragment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.save);
item.setVisible(false);
}
// create Boolean variable in the main activity
private var menuvisibile: Boolean = true
// while navigating fragments set the menuvisibile value and use it
// variable as part of the return statement
invalidateOptionsMenu()
menuvisibile = false
override fun onCreateOptionsMenu(menu: Menu?): Boolean
{
val menuInflater = menuInflater
menuInflater.inflate(R.menu.notification,menu)
return menuvisibile
}
working well for me.

Hiding some action items in ActionBarSherlock in some activities

I am not getting how to show only selected action items on some activities. Below is my code:
public abstract class BaseActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
Intent loginIntent = new Intent(BaseActivity.this,LoginForm.class);
startActivity(loginIntent);
return true;
case 1:
Intent saveIntent = new Intent(BaseActivity.this,SaveForm.class);
startActivity(saveIntent);
return true;
}
return false;
}
}
In the above code I am setting action items so that I can reuse this action items in some activities without repeating the code. Whenever I want these action items to be displayed on particular activity, I am extending this BaseActivity on the present activity as below.
public class FirstActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
....
....
}
So, whenever I need these action items on the particular activities, I am extending the baseactivity to the activities on which I need. Now my problem is how to set only selected action items on particular activity. Suppose I don't want to show up "login" action Item on one activity. How can I get this done? Code snippets would be appreciated.
Now it's a reasonable approach but I would only have items that were reused for all in the baseactivity. That said I think the correct way of handling this would be to override the methods with the implementation you want in these other activities.
You could have some variables that is set in onCreate(), if these are true/false handle it accordingly in onCreateOptionsMenu()
Edit
To override in your own activity you simply do the same thing as in the baseactivity but without calling initializing those buttons you don't need.
If you instead want to make toggle if buttons should be there or not simply add the variables to your baseactivity:
protected boolean mIsLoginButton = true;
Change the onCreateOptionsMenu() method in baseactivity to something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(mIsLoginButton) {
menu.add("Login")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
menu.add("Save")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Then in your firstActivity you simply do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIsLoginButton = false;
}

Categories

Resources