Opening new activities from Popup menu items - android

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

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.

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

Hide Action bar onCreateOptionMenu icon for some activites and fragment android

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

Android ActionBarSherlock On Top Icon Back

I'm trying to make my top action bar icon to allow users to go back to previous screen. I tried to implement these codes. But none are working. Can anyone please guide me on this. I know this looks simple, I'm new to android . Below are my codes.
Problem : When i tap on the icon button it just cleared my screen without going to the previous screen.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
checkInternetConnection();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<--THIS
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, SingleViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the way I do it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
In your ressources(res)
go to menu
and add
this make sur u have some button back for in your drawable
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/back"
android:icon="#drawable/back1"
android:showAsAction="always|withText"
android:title="back"/>
now in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case R.id.back:
Intent in = new Intent(this, <classname which you want to go back>.class);
startActivity(in);
break;
}
return false;
}
you may try this code
<item
android:id="#+id/back"
android:icon="#drawable/btn_back"
android:showAsAction="always|withText"
android:title="#string/txt_back"/>
>
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case R.id.back:
back_action();
return true;
default:
break;
}
return true;
}
>

Categories

Resources