public void onPopup(View view)
{
final PopupMenu menu=new PopupMenu(this,view);
menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast toast=Toast.makeText(MainActivity.this,
item.getTitle()+"Selected",Toast.LENGTH_SHORT);
//Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
//startActivity(intent2);
//startActivity(new Intent(MainActivity.this,YourSpotActivity.class));
toast.show();
return true;
}
});
menu.show();
}
When i click any one of the list item then it will start another activity.
How can i do that by modify an above code. explain me please.
I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.
You need to use switch as below
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
// start activity 1
return true;
case R.id.menuitem2:
Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
// start activity 2
return true;
default:
//default intent
return true;
}
http://developer.android.com/reference/android/widget/PopupMenu.html
You can use switch statement as below inside onMenuItemClick:
switch (item.getItemId()) {
case R.id.menuitem1:
//calling intent ( activity1 )
case R.id.menuitem2:
//calling intent ( activity 2)
default:
//default intent
}
Related
Instead of using toast , i want to use on click listener in the menu items, and can we use fragments in this case
this is the following code in which i want to add on click, so i can open in a new activity
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Yes you can do that by simply adding an Intent against each menu item within switch case. Have a look on the below snippet for your reference:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Ex: launching new activity/screen or show alert message
Intent intent = new Intent(yourActivity.this, NextActivity.class)
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_save:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_search:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_share:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_delete:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
case R.id.menu_preferences:
Intent intent = new Intent(currentactivity.this, toactivity.class)
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Intent
You have to use intent for moving from one screen to another
Intent intent = new Intent(currentactivity.this,towhichactivityyouwantmove.class)
startActivity(intent);
I have an activity menu and on one fragment I want to replace an item inside that menu. The item when pressed launches an activity. My fragment menu item works, but it also calls the activity menu item intent. I need to remove the duplicate main activity menu item being selected too.
Main Activity menu:
public boolean onOptionsItemSelected(MenuItem item) {
else if(itemId == R.id.action_settings)
startActivity (new Intent(getApplicationContext(),
PreferencesActivity.class));
return super.onOptionsItemSelected(item);
}
Fragment menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getActivity(),
PreferencesFragment.class));
return false;
default:
break;
}
return false;
}
It is only if the activity doesn't use the action that it is passed to the fragment. You may be able to add in the activity a boolean that causes the switch to be bypassed for that item.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!isMyFragmentPresent()) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(),
PreferencesActivity.class));
return true;
default:
break;
}
}
return false;
}
public void setMyFragmentIsPresent(boolean isMyFragmentPresent) {
this.isMyFragmentPresent = isMyFragmentPresent;
}
UPDATE: I realise this doesn't work if you have more items than the one you want to change. Maybe you would be better having a default Fragment that loads in the activity that does nothing but load the R.id.action_settings settings button and what it does. This fragment would then be replaced by your new Fragment.
how can I link my setting buttons in the action bar with a another activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
// I want to link this with my setting screen
break;
}
return super.onOptionsItemSelected(item);
}
I linked up my about action bar button with a dialog box but for my settings, I want it to go to another activity. How can I do this? The activity I want to link to is called settings.java
How about this?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
startActivity(this, settings.class);
// This doesn't work?
break;
}
return super.onOptionsItemSelected(item);
}
I have implemented the Sliding Menu in my App. Source: https://github.com/johnkil/SideNavigation
It works like it should but when I click on any item in my menu, the click wont work some reason. I added onClick listener and all that.
Code snippet:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
case R.id.side_navigation_menu_item2:
Intent intent = new Intent(this, DiffAdapter.class);
this.startActivity(intent);
break;
case R.id.side_navigation_menu_item3:
invokeActivity(getString(R.string.title3), R.drawable.ic_action_storage);
break;
case R.id.side_navigation_menu_item4:
invokeActivity(getString(R.string.title4), R.drawable.ic_action_settings);
break;
case R.id.side_navigation_menu_item5:
invokeActivity(getString(R.string.title5), R.drawable.ic_launcher);
break;
default:
return;
}
finish();
}
Any help would be nice. It just wont do anything when I click on a item.
Thanks
I forgot to implement some strings, which caused bugs. Got this solved by adding:
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.ribbon_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
to my code: onCreate ()
i have created an options menu for my database class. Upon launching the options menu, I would like to the desired activity at the click of the specified button.
But the issue is that if i click on any option , i get directed to the MainMenu.class . Any ideas why this is happening ?
code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.optionmenu, menu);
return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
}
case R.id.takesurvey:
{
Toast toast=Toast.makeText(this, "check", 2000);
toast.show();
Intent r1=new Intent(Database.this,SurveyActivity.class);
startActivity(r1);
}
case R.id.viewstats:
{ Intent r2=new Intent(Database.this,Stats.class);
startActivity(r2);
}
case R.id.changesort:
{ Intent r3=new Intent(Database.this,MainMenu.class);
startActivity(r3);
}
case R.id.menuexit:
{ Intent r4=new Intent(Database.this,MainMenu.class);
startActivity(r4);
}
}
return true;
}
It looks like you are missing a break statement in every case.
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.takesurvey:
Toast.makeText(this, "check", 2000).show();
startActivity(new Intent(Database.this,SurveyActivity.class));
break;
case R.id.viewstats:
startActivity(new Intent(Database.this,Stats.class));
break;
case R.id.changesort:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.menuexit:
startActivity(new Intent(Database.this,MainMenu.class));
break;
return true;
}
For each of your conditions in the Switch statement in onOptionsItemSelected() you must return true. If you handle the case then you must return true, if you don't then you should call the super class implementation of it.
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
return true;
}
Go through this for more details
http://developer.android.com/guide/topics/ui/menus.html#options-menu