add a menu to my application with android - android

I created a file option_menu.xml: in the directory /res/menu/
I created a file menu.java that contains this code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
where I can put this code for my menu works?(In menu.java?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nelp:
help();
return true;
case R.id.quit:
quit();
default:
return super.onOptionsItemSelected(item);
}
}
My application contains 4 java files
I Hope the menu to be accessible at any Activity.
Thank you in advance

If you want the same code to be run in several activities, you can create a base class that derives from Activity and then derive your own Activity classes from your new base class. This code would go in that class.
public class ActivityBase extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nelp:
help();
return true;
case R.id.quit:
quit();
default:
return super.onOptionsItemSelected(item);
}
}
}
Your activities derive from ActivityBase:
public class MyActivity extends ActivityBase { ...

Related

Fragment Menuitem Click

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragement, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.color_menu:
intent.setClass(rootView.getContext(), CandleColorActivity.class);
getActivity().startActivityForResult(intent,COLOR_ACTION);
break;
}
return super.onOptionsItemSelected(item);
}
Using the above code menu item is visible in the fragment but item click is not working. Menu item xml:
<item
android:title="selectColor"
android:icon="#drawable/addcolor"
app:showAsAction="always"
android:id="#+id/color_menu"></item>
main activity menu xml always show on each of the fragment
<item
android:title="menu"
android:icon="#drawable/menu"
app:showAsAction="always"
android:id="#+id/uper_menu"></item>
Main activity menu java code open a dialog box on the item click
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.uper_menu:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.menu_dialog);
alertDialog = builder.show();
alertDialog.getWindow().setGravity(Gravity.BOTTOM);
alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
alertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
viewIds();
break;
default:
break;
}
return true;
}
You should not call the super class' onOptionsItemSelected(), if you handled the event yourself. So change your method to this:
public boolean onOptionsItemSelected(MenuItem item) {
...
switch (item.getItemId()) {
case R.id.color_menu:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT
In fragment and activity, only return true, if you handled the event, otherwise return super.onOptionsItemSelected(item);
Reason is, that the system first asks the activity to handle the event, and if the activity says it did handle it (by returning true), the system doesn't ask the fragment anymore.

BaseActivity with common toolbar and menuItems activities can not catch onMenuItemClick

I've the base activity where all toolbar initializations and options menu are done, the activities extending the base can not fire onitemclick
In the base i have
public class BaseActivity extends AppCompatActivity {
private MenuItem refresh;
public Toolbar getToolbar() {
return toolbar;
}
public MenuItem getRefresh() {
return refresh;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
refresh = menu.findItem(R.id.action_refresh);
refresh.setActionView(R.layout.menu_item_view);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return false;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (mState) {
case Const.STATE_IDLE:
refresh.setVisible(true);
break;
case STATE_WORKING:
refresh.setVisible(false);
break;
default:
refresh.setVisible(true);
break;
}
return super.onPrepareOptionsMenu(menu);
}
}
In one of the activites I handle it like
public class CommentsActivity extends BaseToolbarActivity
{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.action_refresh){
setState(Const.STATE_WORKING);
showMsg(contentRoot,"oops");
return true;
}
return super.onOptionsItemSelected(item);
}
}
but the click items won't fire
After a while I came to realize that since I was setting a custom layout for my items (useful for animations) an option menu with a custom view that is either set in the xml or dynamically using
item.setActionView(R.layout.menu_lay);
Just like my problem above the menu item can never be invoked by the normal onOptionsItemSelected listener so the way to make it work is to implement onClickListener on the item's custom View so in my case the way to make it invoke is by
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getRefresh.getActionView().setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//refresh some data
}
});
return true;
}

How to hide an optionsmenu button at runtime

In my app i have an optionsmenu. It has 2 buttons. Depending on a boolean value i would like to show/hide one of the buttons. I've got the following code but it doesn't hide the button. How can i do this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menushowmoredetails, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(displayRotaDetails.equalsIgnoreCase("false")){
if(item.getItemId() == R.id.moredetails)
item.setVisible(false);
}
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
return true;
case R.id.moredetails:
You have to use the onPrepareOptionMenu method like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// add your conditions here and change 0 with the R.id.moredetails item postion.
if(displayRotaDetails.equalsIgnoreCase("false")){
menu.getItem(1).setVisible(false);
}
}

Android-Problem in Menu of Activity in Activity Group

I have used a technique (http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity)
to develop an app where I have 3 tabs and each tab has its own ActivityGroup. I have menus for each activity. But when I press menu button, the menu does not appear. After doing some random trails I found that If I implement onCreateOptionsMenu in ActivityGroup then only menu appears. I am not able to execute onCreateOptionsMenu of Activity.
Please suggest how to use menu of Activity as I have many activities in single ActivityGroup and by implementing onCreateOptionsMenu in ActivityGroup is not the right way to handle this problem.
Here is how you roll with it:
In your ActivityGroup class onCreateOptionMenu() call the current Activity 's onCreateOptionMenu() i.e
public boolean onPrepareOptionsMenu(Menu menu)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onPrepareOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
Activity activity = getLocalActivityManager().getCurrentActivity();
return activity.onOptionsItemSelected(item);
}
and in your individual Activity
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId())
{
case R.id.MENU_LOGOUT:
Dialog.showToast(this, "message");
return true;
case R.id.MENU_HELP:
break;
case R.id.MENU_ABOUT:
break;
}
return super.onOptionsItemSelected(item);
}
and if you want any Activity without having any Menu just override these methods
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return true;
}

Menu doesn't appear after linking to another page with phonegap

I use phonegap,
When linking to another page with simple href my menu stop working.
In my Java class I override onKeyDown() and return false for menu button
#Override
public boolean onKeyDown(int i,KeyEvent e){
if (i==KeyEvent.KEYCODE_MENU){
return false;
}else{
return true;
}
}
When I loadUrl from the java class I can open the menu (in about.html)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.exit:
this.finish();
return true;
case R.id.about:
super.loadUrl("file:///android_asset/www/about.html");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources