Menu Item Select Problems - android

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

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.

Where to add menu to work in all activities

I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android

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

actionbar and wrong button pressed

hello i have an actionBar with this menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_open"
android:title=".."
android:icon="#drawable/open_holo_light"
yourapp:showAsAction="always|withText" />
<item android:id="#+id/menu_delete"
android:title=".."
android:icon="#drawable/delete_holo_light"
yourapp:showAsAction="always|withText" />
<item android:id="#+id/menu_detail"
android:title=".."
android:icon="#drawable/info_holo_light"
yourapp:showAsAction="always|withText" />
</menu>
this is when i insert my menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_menu, menu);
return true;
}
here i press my menu items:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_open:
Toast.makeText(this, "open", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_detail
Toast.makeText(this, "detail", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
the problem is easy! if i press menu_open i have no toast, if i press menu_delete i have open toast, if i press menu_detail i have delete toast.. why? thanks!
Remove this line:
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
And change every yourapp:showAsAction with android:showAsAction.

ActionBarSherlock requesting search

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

Categories

Resources