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.
Related
I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}
I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?
Following are my code snippets.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressLint("NewApi") #Override
public boolean onOptionsItemSelected(MenuItem item) {
View menuItemView = findViewById(R.id.action_button);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.popup);
popupMenu.show();
return true;
}
and my popup menu is as follows,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/one"
android:title="About"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
<item
android:id="#+id/two"
android:title="Contact Us"
android:visible="true"
android:showAsAction="ifRoom|withText"/>
</menu>
What i want to do is, when i click on these menu items another activities has to be opened. How could i do that?
Can someone help me please. Thanks in advance.
Use the id to launch the activity using switch statement with menu itemId
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.one:
Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
startActivity(intent1);
return true;
case R.id.two:
Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try this
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
To open activity on popup menu click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
A sliding menu was implemented using google navigation drawer with actionbar class. My problem is onCreateOptionsMenu is being shown in every activities. how can i make onCreateOptionsMenu icon visible and invisible at will. Any idea please.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Either you create menu runtime or have different menu layouts as per your need and in onCreateOptionsMenu of your activity set that layout or runtime create those menus or if you want to show no menu icons then just do menu.clear()
Activity A
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_a, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Activity B
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.clear();
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_b, menu);
// OR
Drawable tmpDrawable = getResources().getDrawable(R.drawable.share_sharingicon);
// tmpDrawable.setColorFilter(getResources().getColor(R.color.colorGrayFont), PorterDuff.Mode.MULTIPLY);
menu.add("ShareMap").setIcon(tmpDrawable).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Directions").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ShareMap:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
case R.id.Directions:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
To hide or show some icon on action bar, you need override method:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem your_icon = menu.findItem(R.id.action_your_icon);
//show icon
your_icon.setvisible(true);
//hide icon
your_icon.setvisible(false);
...
}
Besides you need 'supportInvalidateOptionsMenu()' to Invalidate the activity's options menu when action bar items have some change
I have one action bar activity.when i am switching from one fragment to another fragment that fragment have two menus like setting,gridview.When i click the setting it perform the action but not in gridview.In each fragment first menu item only perform action not in second fragment.Please give me solution thanks.....
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.settingmenu, menu);
getActivity().getMenuInflater().inflate(R.menu.menugridcalendar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_setting:
Log.i(TAG,"InsdieGridmenu");
Intent intent = new Intent(getActivity(), SettingPage.class);
startActivity(intent);
break;
case R.id.gridmenuid:
Activity activity = getActivity();
if (activity instanceof ListItemClickListener) {
((ListItemClickListener)activity).OpenGridView();
}
break;
}
return true;
}
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);
}
}