Hi i have Use Menu in 2 class this is my Menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/setting"
android:icon="#drawable/ic_seting"
android:title="Setting">
</item>
</menu>
in class A i have use this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options, menu);
return true;
}
public boolean onCreateOptionsMenu(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.SavedList:
Intent intent = new Intent(
A.this,
SetPreference.class);
startActivity(intent);
return true;
}
return true;
}
and in class b i have use
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options, menu);
return true;
}
public boolean onCreateOptionsMenu(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.SavedList:
Intent intent = new Intent(
b.this,
SetPreference.class);
startActivity(intent);
return true;
}
return true;
}
in one class b its working fine but in class its not working please tell me where i m doing wrong please help me i am new n android
When you are using Intent that time why need two different class with same body. What I suggest is In case body write a Intent for class B menu.. see whether work or not .
Go through the documentation of the Intent in Android and Also menu Android documentation you will find more information .
Related
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.
Hi I added the menu file ,there are 3 Menu Items:
1) Menu Pin,
2) Send Reply and
3) Add Notes.
Send Reply , Add Notes are working fine but, when removed comment for pin, it is not working. I attached the code below.
Please suggest me any solution. When I paste the code of Send Reply to pin_menu case for testing purpose it is not working.
Log cat doesn't show anything.I added the toast on click of pin_menu it doesn't show.Please suggest me solution.
I also tried to add one extra menu in XML file and added the code same as the pin_menu but not worked. Doesn't show log cat ,toast. So that hard to debug.Same for Send Reply and Add Note but both are working fine.
Code is as below:
<item android:id="#+id/menu_pin"
android:icon="#drawable/pin"
android:title="#string/pin"
android:showAsAction="never"
/>
<item android:id="#+id/menu_send_reply"
android:icon="#drawable/send"
android:title="#string/send_reply"
android:showAsAction="never"
/>
<item android:id="#+id/menu_add_note"
android:icon="#drawable/add_note"
android:title="#string/add_note"
android:showAsAction="never"
/>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.ticket_properties_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("DATA ","Item ID "+item.getItemId());
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.menu_add_note:
Intent i3 = new Intent(Ticket_properties.this,Add_note.class);
i3.putExtra("ID", Ticket_id);
i3.putExtra("client_id", client_id);
startActivity(i3);
return true;
case R.id.menu_send_reply:
Intent reply= new Intent(Ticket_properties.this,Send_reply.class);
reply.putExtra("ticket_id", Ticket_id);
reply.putExtra("title", Ticket_title);
reply.putExtra("dept_id", tv_dept_id.getText());
reply.putExtra("Ticket_hash", Ticket_hash);
reply.putExtra("filter_id",filter_id);
startActivity(reply);
return true;
case R.id.menu_pin:
Intent reply1= new Intent(Ticket_properties.this,Send_reply.class);
reply1.putExtra("ticket_id", Ticket_id);
reply1.putExtra("title", Ticket_title);
reply1.putExtra("dept_id", tv_dept_id.getText());
reply1.putExtra("Ticket_hash", Ticket_hash);
reply1.putExtra("filter_id",filter_id);
startActivity(reply1);
return true;
/*
String PIN_URL=op.getUrl(Ticket_properties.this,"ticket", "add_pinup","&vis_ticket_id=124");
JSONArray pin_result = JSONfunctions.getJSONfromURL(PIN_URL+"&vis_encode=json",Ticket_properties.this);
String result =pin_result.toString();
if(result.equals("[\"success\"]"))
{
Operation.showToast(getApplicationContext(),R.string.pinned);
}
*/
default:
return super.onOptionsItemSelected(item);
}
}
Well i think you are using this commented code on the wrong place. You should use it before return, in the switch case structure. If you want to trigger it on pin clicked. Here is the updated code;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.ticket_properties_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("DATA ","Item ID "+item.getItemId());
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.menu_add_note:
Intent i3 = new Intent(Ticket_properties.this,Add_note.class);
i3.putExtra("ID", Ticket_id);
i3.putExtra("client_id", client_id);
startActivity(i3);
return true;
case R.id.menu_send_reply:
Intent reply= new Intent(Ticket_properties.this,Send_reply.class);
reply.putExtra("ticket_id", Ticket_id);
reply.putExtra("title", Ticket_title);
reply.putExtra("dept_id", tv_dept_id.getText());
reply.putExtra("Ticket_hash", Ticket_hash);
reply.putExtra("filter_id",filter_id);
startActivity(reply);
return true;
case R.id.menu_pin:
String PIN_URL=op.getUrl(Ticket_properties.this,"ticket", "add_pinup","&vis_ticket_id=124");
JSONArray pin_result = JSONfunctions.getJSONfromURL(PIN_URL+"&vis_encode=json",Ticket_properties.this);
String result =pin_result.toString();
if(result.equals("[\"success\"]"))
{
Operation.showToast(getApplicationContext(),R.string.pinned);
}
Intent reply1= new Intent(Ticket_properties.this,Send_reply.class);
reply1.putExtra("ticket_id", Ticket_id);
reply1.putExtra("title", Ticket_title);
reply1.putExtra("dept_id", tv_dept_id.getText());
reply1.putExtra("Ticket_hash", Ticket_hash);
reply1.putExtra("filter_id",filter_id);
startActivity(reply1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I want to add a clickable icon on the default title bar looks like
edited with
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title is needed");
menuItem.setIcon(R.drawable.call);
return true;
}
Looks like
Override method onCreateOptionsMenu in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title if needed");
menuItem.setIcon(R.drawable.YOUR_ICON_HERE);
// the rest of the code here...
}
In fact if you are planning to use this action bar on old platforms as well (2.x-3.x), you better consider using ActionBarSherlock
Check this code,
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
menu.add("Search")
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent search = new Intent(MainActivity.this,
SearchActivity.class);
startActivityForResult(search, 0);
overridePendingTransition( R.anim.righttoleft, R.anim.stable );
return false;
}
}).setIcon(R.drawable.ic_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
I have written one application. I want to show the settings option on the login screen of an application. Now i have an activity LoginActivity as shown below
class LoginActivity extends BaseLoginActivity
{
showLoginDialog();
/* some code here*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
populateMenu(menu);
return super.onCreateOptionsMenu(menu);
}
protected void populateMenu(Menu menu) {
menu.add(Menu.NONE, SETTINGS, Menu.NONE, "Settings").setIcon(
android.R.drawable.ic_menu_manage);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SETTINGS:
startSettingActiviy();
return (true);
}
return (super.onOptionsItemSelected(item));
}
public void startSettingActiviy() {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
/* some code here*/
}
when i press on the menu option i cannot see the menu. I tried to debug the code, it is not even reaching the oncreateOptionsMenu function. Please let me know what is missing here.
try this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
populateMenu(menu);
return true;
}
and this too
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SETTINGS:
startSettingActiviy();
return (true);
}
return true;
}
hope this will help you:)
Try out this way:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
populateMenu(menu);
return true;
}
Give it a try with onPrepareOptionsMenu(Menu menu) instead
Also,
Minimum SDK version could be the cause. If you reduce it to 13-, you should probably see the menu show up again. Good article on this subject: POST
Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...
MenuTest.java
public class MenuTest extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.feeds:
break;
case R.id.friends:
break;
case R.id.about:
break;
}
return true;
}
}
And my XML file is more_tab_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/feeds"
android:title="Feeds"/>
<item
android:id="#+id/friends"
android:title="Friends"/>
<item
android:id="#+id/about"
android:title="About"/>
</menu>
Please guide me,
public class MenuTest extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
// return true so that the menu pop up is opened
return true;
}
}
and don't forget to press the menu button or icon on Emulator or device
please see :==
private int group1Id = 1;
int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);
menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);
menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);
menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);
menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);
menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
// write your code here
Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);
msg.show();
return true;
case 2:
// write your code here
return true;
case 3:
// write your code here
return true;
case 4:
// write your code here
return true;
case 5:
// write your code here
return true;
case 6:
// write your code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Change your onCreateOptionsMenu method to return true. To quote the docs:
You must return true for the menu to be displayed; if you return false it will not be shown.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.folderview_options, menu);
return (super.onCreateOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.locationListRefreshLocations) {
Cursor temp = helper.getEmployee(active_employeeId);
String[] matches = new String[1];
if (temp.moveToFirst()) {
matches[0] = helper.getEmployerID(temp);
}
temp.close();
startRosterReceiveBackgroundTask(matches);
} else if (item.getItemId()==R.id.locationListPrefs) {
startActivity(new Intent(this, PreferencesUnlockScreen.class));
return true;
}
return super.onOptionsItemSelected(item);
}
you can create options menu like below:
Menu XML code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/Menu_AboutUs"
android:icon="#drawable/ic_about_us_over_black"
android:title="About US"/>
<item
android:id="#+id/Menu_LogOutMenu"
android:icon="#drawable/ic_arrow_forward_black"
android:title="Logout"/>
</menu>
How you can get the menu from MENU XML(Convert menu XML to java):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
How to get Selected Item from Menu:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Menu_AboutUs:
//About US
break;
case R.id.Menu_LogOutMenu:
//Do Logout
break;
}
return super.onOptionsItemSelected(item);
}
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class AndroidWalkthroughApp2 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// show menu when menu button is pressed
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// display a message when a button was pressed
String message = "";
if (item.getItemId() == R.id.option1) {
message = "You selected option 1!";
}
else if (item.getItemId() == R.id.option2) {
message = "You selected option 2!";
}
else {
message = "Why would you select that!?";
}
// show message via toast
Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
toast.show();
return true;
}
}
Replace return super.onCreateOptionsMenu(menu); with return true; in your onCreateOptionsMenu method
This will help
And you should also have the onCreate method in your activity
The previous answers have covered the traditional menu used in android. Their is another option you can use if you are looking for an alternative
https://github.com/AnshulBansal/Android-Pulley-Menu
Pulley menu is an alternate to the traditional Menu which allows user to select any option for an activity intuitively. The menu is revealed by dragging the screen downwards and in that gesture user can also select any of the options.
Android UI programming is a little bit tricky. To enable the Options menu, in addition to the code you wrote, we also need to call setHasOptionsMenu(true) in your overriden method OnCreate().
Hope this will help you out.
IF your Device is running Android v.4.1.2 or before,
the menu is not displayed in the action-bar.
But it can be accessed through the Menu-(hardware)-Button.
Good Day
I was checked
And if You choose Empty Activity
You Don't have build in Menu functions
For Build in You must choose Basic Activity
In this way You Activity will run onCreateOptionsMenu
Or if You work in Empty Activity from start
Chenge in styles.xml the