menu won't show up android - android

Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !

This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.

Related

Menu items keeps on adding on every menu button press

I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="#drawable/search_white_24dp"
android:id="#+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="#drawable/settings_white_24dp"
android:title="Setting"
android:id="#+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="#drawable/share_white"
android:id="#+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="#drawable/settings_white_24dp"
android:id="#id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public void onCreateOptionsMenu(Menu menu ) {
menu.clear();// use menu.clear
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu."your current activity name ", menu);
return true;
}
use menu.clear()
The best to do is as this :
#Override
public void onCreateOptionsMenu(Menu menu ) {
getMenuInflater().inflate(R.menu."your current activity name ", menu);
return true;
}
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.

Android onCreateOptionsMenu not called when hitting the menu button

I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"

ShareActionProvider Not Clickable

I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto=" >
<item android:id="#+id/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.

How to create an Overflow menu and combine it with an option menu?

I am new in Android development. I want to ask how can I create an overflow menu with two items. Also, how can I combine it with an option menu I already did, which looks like this:
public class MainActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater blabla = getMenuInflater();
blabla.inflate(R.menu.options_menu ,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.OurSponsors:
Intent i = new Intent("com.--.------.------");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
xml
<item
android:title="Our Sponsors"
android:id="#+id/OurSponsors"
android:icon="#drawable/image"
/>
<item
android:title="Exit"
android:id="#+id/exit"
android:icon="#drawable/exit"
/>
I am a beginner.
Here is the developer page related to that http://developer.android.com/guide/topics/resources/menu-resource.html.
You will want to add android:showAsAction="collapseActionView" (or android:showAsAction="never" depending on your API level) to each menu item in the XML layout

Android - Menu Item cannot be resolved to a type in onOptionsItemSelected()

I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine. When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing. And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up. The menu is also in the correct folder (I actually just left it as is when creating the Activity). Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="#string/add_screen_ok"
android:icon="#drawable/accept_icon" />
<item
android:id="#+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="#string/add_screen_cancel"
android:icon="#drawable/cancel_icon" />
</menu>
Here's the code
public class AddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Change
android.R.id.home:
to
R.id.home:
and the same with the other one.
android.R is for sdk resources and R.id.some_id is for ids you create

Categories

Resources