ActionBarSherlock requesting search - android

I`m trying to request search from ActionBarSherlock.
My class extends SherlockListActivity.
This is how I`m adding Menu button to the ActionBar:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
The problem is that I am unable to invoke it by id:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case ?????: {
onSearchRequested();
return true;
}
}
return true;
}
How people usually solve this problem?
Thanks.

If you inflate you menu from an XML menu file you will set an id which you can then use in your onOptionsItemSelected method. Like this:
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" android:title="#string/search"/>
</menu>
Fragment
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Also you can use
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1, "Refresh").setIcon(R.drawable.ic_refresh).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(0, 2, 2, "Search").setIcon(R.drawable.ic_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()){
case 1:
...
break;
case 2:
...
break;
}
return true;
}

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.

Menu Item Select Problems

This is my NavagatorView code,
<item
android:icon="#drawable/ic_home_black_24dp"
android:title="Main"
android:id="#+id/Main"
/>
<item
android:icon="#drawable/ic_wc_black_24dp"
android:title="Matched"
android:id="#+id/Matched"
/>
This is my click register code,
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.Main:
//newGame();
return true;
case R.id.Matched:
displayInfoDialogView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I want it so when I click "Matched" the displayInfoDialogView(); runs.
Now when I run it nothing happens.
add this code below in your onCreateView method:
setHasOptionsMenu(true);

Action Bar items not working when clicked

I'm having an unusual issue here. My action bar was working properly then i went an tested it now and it totally stop. When pressed, they give no response. One is a back button and the other is a send button. Both of which aren't working. Here is my code for the Menu
ActivityOne.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed();
return true;
case R.id.action_send:
new PostUpLoad().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_send.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityOne">
<item
android:id="#+id/action_send"
android:orderInCategory="100"
android:title="#string/send"
android:icon="#drawable/ic_send"
app:showAsAction="ifRoom" />
</menu>
Everything seems fine but they aren't working at all. Any help would be greatly appreciated.
Replace R.id.home with android.R.id.home.
You're missing breaks in your switch case, and I'm not sure about returning true in onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed(); //might use finish() instead
break;
case R.id.action_send:
new PostUpLoad().execute();
break;
default:
super.onOptionsItemSelected(item);
}
}

Change menu item after selection in Android

I am trying to replace the menu item to another menu item after selection. I tried the following but it is not working as expected. Any solutions to this. thanks.
The menu.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/add" android:visible="true" android:enabled="true" android:title="Add"></item>
<item android:id="#+id/delete" android:visible="false" android:enabled="true" android:title="Delete"></item>
</menu>
The code inside MyActivity.java is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.defaultmenu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(isAdded) {
menu.removeItem(R.id.add);
menu.add(0, R.id.delete, 0, "Delete");
} else {
menu.removeItem(R.id.delete);
menu.add(0, R.id.add, 0, "Add");
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add:
isLogin = true;
return true;
case R.id.delete:
isLogin = false;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To refresh your menu call invalidateOptionsMenu();
And i guess in onPrepareOptionsMenu, you can do..
if(isAdded) {
menu.findItem(R.id.add).setVisible(false);
menu.findItem(R.id.delete).setVisible(true);
return true;
} else {
menu.findItem(R.id.add).setVisible(true);
menu.findItem(R.id.delete).setVisible(false);
return true;
}
return super.onPrepareOptionsMenu(menu);
Try like this
public static final int ADD_CATEGORY_INDEX = Menu.FIRST;
public static final int DELETE_CATEGORY_INDEX= Menu.FIRST+1;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADD_CATEGORY_INDEX, 0, "Add");
menu.add(0, DELETE_CATEGORY_INDEX, 0, "delete");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ADD_CATEGORY_INDEX:
break;
}
case DELETE_CATEGORY_INDEX:
break;
}
return true;
}
I Changed the onPrepareOptionsMenu, now it's working fine.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//menu.clear();
if(isadded) {
menu.removeItem(R.id.add);
menu.removeItem(R.id.delete);
menu.add(0, R.id.delete, 0, "Delete");
} else {
menu.removeItem(R.id.add);
menu.removeItem(R.id.delete);
menu.add(0, R.id.add, 0, "Add");
}
return super.onPrepareOptionsMenu(menu);
}

Android, How to create option Menu

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

Categories

Resources