How to have 2 OptionsItemSelected? - android

In my Activity, I have two icon button in action bar
This is in add_task.xml
<item
android:id="#+id/action_add_task"
android:icon="#drawable/create_new"
android:title="Add New"
app:showAsAction="always" />
<item
android:id="#+id/menu"
android:icon="#drawable/menu"
android:title="Menu"
app:showAsAction="always" />
When the icon menu is clicked, I get this
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/opt1"
android:icon="#drawable/change_pic"
android:title="Change Profile" />
<item
android:id="#+id/opt2"
android:icon="#drawable/sign_out"
android:title="Sign Out" />
</menu>
My problem is that the Toast get displayed when the menu icon clicked. I want it only display when the change profile is clicked.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_task, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
View menuItemView = findViewById(R.id.menu);
MenuBuilder menuBuilder =new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.popup, menuBuilder);
MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
optionsMenu.setForceShowIcon(true);
optionsMenu.show();
case R.id.opt1: // when change profile clicked
Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}
Edit
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
View menuItemView = findViewById(R.id.menu);
MenuBuilder menuBuilder = new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.popup, menuBuilder);
final MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
opionsMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Change Profile".equals(item.getTitle())) {
Intent intent = new Intent(AddMonthlyExpenses.this, Profile.class); // go to Information class
intent.putExtra("name", name);
startActivity(intent);
}
optionsMenu.setForceShowIcon(true);
optionsMenu.show();
return true;
}
});
default:
return super.onOptionsItemSelected(item);
}
return true;
}
I get error on this line
opionsMenu.setOnMenuItemClickListener
optionsMenu cannot be solved.

You have not added Break statement
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.menu:
View menuItemView = findViewById(R.id.menu);
MenuBuilder menuBuilder =new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.popup, menuBuilder);
MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
optionsMenu.setForceShowIcon(true);
optionsMenu.show();
break;
case R.id.opt1: // when change profile clicked
Toast.makeText(getApplication(),"Profile",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
try this code

Finally I found the answer
case R.id.menu:
View menuItemView = findViewById(R.id.menu);
MenuBuilder menuBuilder = new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.popup, menuBuilder);
MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
optionsMenu.setForceShowIcon(true);
// Set Item Click Listener
menuBuilder.setCallback(new MenuBuilder.Callback() {
#Override
public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
switch (item.getItemId()) {
case R.id.opt1: // Handle option1 Click
Intent intent = new Intent(AddMonthlyExpenses.this,Profile.class);
startActivity(intent);
return true;
case R.id.opt2: // Handle option2 Click
return true;
default:
return false;
}
}
#Override
public void onMenuModeChange(MenuBuilder menu) {}
});
// Display the menu
optionsMenu.show();

Related

How to show the selected item in popup menu which is inside a fragment using a check box?

I am using an image button for a popup menu to popup everything is working fine but when I select an item in the menu the item is selected and it doesn't show the selection so that I could identify the selected item.The checkbox remains unchecked even after the selection
menu_icon_img=myView.findViewById(R.id.Id_customer_over_flow);
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() != null) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.select_name_z_a:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return false;
}
}
});
}
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<group
android:checkableBehavior="single"
>
<item
android:id="#+id/select_name_a_z"
android:title="#string/name_a_z"
android:checkable="true"
/>
<item
android:id="#+id/select_name_z_a"
android:title="#string/name_z_a"
android:checkable="true"
/>
</group>
</menu>
The problem is you are creating the popup menu from onClick of an imageview. whenever a click event occures a new instance of popup menu is being created.
to avoid this initiate the popup menu in onCreate method. And call popup.show() from onClick() method.
Toolbar menus are supposed to navigate you the specified activities or fragments on click of the pop up.I am not sure what is the problem , but once you click on any MenuItem it will navigate you to given Intent associated with the the id for example,
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
Intent intent=new Intent(MainActivity.this,SecondActivity.this);
startActivity(intent);
return true;
If you are using a checkable menu item then change the below code from
case R.id.select_name_z_a:
if (item.isChecked())
item.setChecked(false);
else item.setChecked(true);
return true;
Do like
if (!item.isChecked()) item.setChecked(true);
Because item.ischecked() is false in the beginning state.
Made some changes to your above code, Try this
menu_icon_img=findViewById(R.id.Id_customer_over_flow);
popup = new PopupMenu(getApplicationContext(), menu_icon_img);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (this != null) {
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Menu menu = popup.getMenu();
for(int i = 0;i<menu.size();i++){
menu.getItem(i).setChecked(false);
}
item.setChecked(true);
return true;
}
});
}
}
});
Also for all items in menu set:
android:checkable="true"

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.

Opening new activities from Popup menu items

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

Back button on actionBar does not work

I have a < button on actionBar beside the app title and a + button.
I create the < button by using code below
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.mipmap.back_to);
and then
#Override
public boolean onCreateOptionsMenu(Menu menu) { // for + button
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.create_new_details, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home: // back to previous activity, < button
onBackPressed();
break;
case R.id.addDetails:
mClickedPosition = -1;
View menuItemView = findViewById(R.id.addDetails);
PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplication(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Add Work Details".equals(item.getTitle())) {
Intent intent = new Intent(getApplication(), Add_Details_Information.class); // go to Details class
startActivityForResult(intent, PROJECT_REQUEST_CODE);
} else {
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
But only the + button works. How do I back to previous activity when < button is clicked?
Just change R.id.home to android.R.id.home.

customize options menu in android

I'd like to customize options menu in my app. After some googling and making a lot of effort I am still unable to do it. Here is what I want:
menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_first"
android:icon="#drawable/phone_selector"
android:title="#string/first"/>
<item
android:id="#+id/menu_second"
android:icon="#drawable/butterfly_selector"
android:title="#string/second"/>
<item
android:id="#+id/menu_third"
android:icon="#drawable/umbrella_selector"
android:title="#string/third"/>
</menu>
OptionsMenuActivity.java:
public class OptionsMenuActivity extends Activity {
private Menu currentMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
this.currentMenu = menu;
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
setMenuItemIconState(menu, MyApplication.getSelectedMenuItemDrawIcon(), false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_phone:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.phone_active);
Intent intent = new Intent(OptionsMenuActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_butterfly:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.butterfly_active);
intent = new Intent(OptionsMenuActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_umbrella:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.umbrella_active);
intent = new Intent(OptionsMenuActivity.this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setMenuItemIconState(Menu menu, int menuItemIconRes, boolean switchLastActiveMenuItemIcon) {
int menuItemId = 0;
int lastDeActiveDrawIcon = 0;
switch (menuItemIconRes) {
case R.drawable.phone_active:
menuItemId = R.id.menu_first;
lastDeActiveDrawIcon = R.drawable.phone;
break;
case R.drawable.butterfly_active:
menuItemId = R.id.menu_second;
lastDeActiveDrawIcon = R.drawable.butterfly;
break;
case R.drawable.umbrella_active:
menuItemId = R.id.menu_third;
lastDeActiveDrawIcon = R.drawable.umbrella;
break;
}
if (switchLastActiveMenuItemIcon) {
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(lastDeActiveDrawIcon);
return;
}
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(MyApplication.getSelectedMenuItemDrawIcon());
}
}
In MyApplication.java I have only a getter and a setter of selectedMenuItemDrawIcon.I managed to set the icons properly, but failed with the others.
Is it possible to customize options menu like that? If not, is there any other approaches for creating an xml file and make it appear and disappear smoothly just like basic menu of android.
Any help would be appreciated. Thanks in advance, (please provide with code example).

Categories

Resources