2016年9月14日 星期三

OPtion Menu

Option menu

how to use
1.Add at the menu/main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context=".MainActivity">
   <item
       android:id="@+id/action_settings"
       android:orderInCategory="100"
       android:title="@string/action_settings"
       app:showAsAction="never" />
   <item
       android:id="@+id/action_refresh"
       android:orderInCategory="10"
       android:title="@string/action_refresh"
       app:showAsAction="ifRoom" />
</menu>

2A  in Activity show the menu

@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;
   }

You must return true for the menu to be displayed; if you return false it will not be shown.

   @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);
   }


2B in Fragment show the menu

@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.forecastfragment,menu);

   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()){
           case R.id.action_refresh:

               break;
       }
       return super.onOptionsItemSelected(item);
   }



why onOptionsItemSelected has a boolean?


down vote
As per documentation
true --> Event Consumed now It should not be forwarded for other event
false --> Forward for other to consume
This Boolean return type actually benefits when we are working with multiple fragments and every fragment has their own Option menu and Overriding of OnOptionItemSelected(Mainly in tablet design)
In this case android trace every fragments OnOptionItemSelected method so to avoid that
a) If any fragment is consuming event in onOptionsItemSelected() so return "true" else return "false"
b) If We return false then It will trace other connected fragment's onOptionsItemSelected)
method until it ends all fragment or Somebody consumes It.
enter image description here
Here I have tried to explain from diagram
Green color boundary is fragment-1 and Red color boundary is fragment-2
both fragment has their own Optionmenu which I have highlighted

Now If we click any of OptionmenuItem It will check Implementation of onOptionsItemSelected() in both fragments

If any fragment is consuming event onOptionsItemSelected return true, By this it would never try for other fragment and we can reduce overhead of Android operation system.


Set the option menu color:
http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2
http://stackoverflow.com/questions/19659637/how-to-change-the-background-color-of-action-bars-option-menu-in-android-4-2/22704653#22704653