i'm not really sure what i'm doing wrong. I am working on a 6 button menu. The buttons Display but do not call the activity, and i cant see any text displayed on the menu button. They appear Blank, Help Please!!
package com.cerealBarApps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class FirstLoginActivity extends Activity {
protected void onCreate(Bundle Ebenezersbundle)
{
super.onCreate(Ebenezersbundle);
setContentView(R.layout.testlayout);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
/*
* Intent nextScreen = new Intent(getApplicationContext(),
* AllFaculty.class); // Sending data to another Activity
* startActivity(nextScreen);
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent intent1 = new Intent(this, SMS.class);
startActivity(intent1);
break;
case 2:
Intent intent2 = new Intent(this, MenuRecieved.class);
startActivity(intent2);
break;
case 3:
Intent intent3 = new Intent(this, MenuSent.class);
startActivity(intent3);
case 4:
Intent intent4 = new Intent(this, MenuSettings.class);
startActivity(intent4);
case 5:
Intent intent5 = new Intent(this, MenuExit.class);
startActivity(intent5);
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="xmlns:android= http:/schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuMenu"
android:alphabeticShortcut="m"
android:title="MenuTest"/>
<item
android:id="#+id/menuNewMessage"
android:alphabeticShortcut="n"
android:title="New Message"/>
<item
android:id="#+id/menuSent"
android:alphabeticShortcut="s"
android:title="Sent"/>
<item
android:id="#+id/menuRecieved"
android:alphabeticShortcut="r"
android:title="Recieved"/>
<item
android:id="#+id/menuSettings"
android:alphabeticShortcut="s"
android:title="Settings"/>
<item
android:id="#+id/menuExit"
android:alphabeticShortcut="e"
android:title="Exit"/>
</menu
Code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuNewMessage:
startActivity(new Intent(getApplicationContext(), SMS.class));
return true;
case R.id.menuSent:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuRecieved:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuSettings:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuExit:
startActivity(new Intent("com.cerealBarApps"));
return true;
}
return false;
}
}
Remove return false and replace it with:
return super.onOptionsItemSelected(menuItem);
follow the below code
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId())
{
case 0:
this.SaveData();
break;
case 1:
Intent intent2 = new Intent(DryWall.this,Help.class);
startActivity(intent2);
break;
default:
break;
}
return super.onOptionsItemSelected(menuItem);
}
Hey guys i figured it out, it was because i didnt do an onCreateOptionsMenu method. Thanks gusy for the help
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, menuNewMessage, 0, "New Message");
menu.add(0, menuSent, 0, "Outbox");
menu.add(0, menuRecieved, 0, "Inbox");
menu.add(0, menuSettings, 0, "Settings");
menu.add(0, menuExit, 0, "Exit");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case menuNewMessage:
startActivity(new Intent(getApplicationContext(), SMS.class));
break;
case menuSent:
startActivity(new Intent(getApplicationContext(), MenuSent.class));
break;
case menuRecieved:
startActivity(new Intent(getApplicationContext(),
MenuRecieved.class));
break;
case menuSettings:
startActivity(new Intent(getApplicationContext(),
MenuSettings.class));
break;
case menuExit:
startActivity(new Intent(getApplicationContext(), MenuExit.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
Related
i am new at android.I am using three dots menu in my android app but when i click on item on on three dots menu..my app crashes.can someone help me here is my code:
enter code here#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
break;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
break;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
break;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
return true;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
return true;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
return true;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Seems there is nothing wrong with the code.
This could be issue with declration in manifest.
Make sure the activities account, help are declared in manifest.
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 am a beginner android programmer ... I want to do something simple:
can I use these function and show my action bar in all the activities without copy these function in every activity's file?
thank you for help
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
Keep these in a parent activity class and extend it inside all your activities instead of extending Activity
Keep a parent activity which has these 2 common methods.
public class ParentActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
}
Extend the parent class in your activity.
public class YourActivity extends ParentActivity {
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to load an activity, when an option is selected from the menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_flash:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
break;
case R.id.menu_color:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
break; break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
but it keep to give me intent error
Change with this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);;
return true;
case R.id.item2:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
return true;
default:
return super.onContextItemSelected(item);
}
}
Intent intent = new Intent(youractivity.this, FlashActivity.class);
startActivity(intent);
Change to above code...
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu_flash:
Intent intent = new Intent(this, FlashActivity.class);
startActivity(intent);
return true;
}
return false;
Have you declared FlashActivity in your manifest file??
if not,add this to your manifest:
<activity
android:name=".FlashActivity" >
</activity>
Make sure that the FlashActivity declare in manifest file.Then try this..
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_flash:
openFlashmenu();
return true;
case R.id.menu_color:
openFlashmenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openFlashmenu(){
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
}
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