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

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

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.

Android don't want to see the OptionMenu

I have added a button in the action bar, it works fine but if I press the physical menu button, the menu appears action_settings. Instead I want to use only the menu in the ActionBar and not that of the physical menu button.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_scegli, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_c:
Intent intent = null;
intent = new Intent(getActivity(), Inserisci_.class);
startActivity(intent);
return true;
case R.id.action_settings:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
You can try overriding onKeyDown method in your activity and returning true if the menu key was pressed - this will effectively prevent android from internally handling the menu key:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
else {
return false;
}
}

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

add a menu to my application with 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 { ...

Categories

Resources