-1

I have made an app with a lot of buttons and activities. I'm having trouble though understanding how to start a new activity through a button that is in my menu (when the menu button is clicked on phone) (inflatable menu). This is my code for the menu connected to my activity:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1" android:title="@string/menu_home"></item>
</menu>

Here is my activities in Java:

package com.gmail.derekcraigsmith.nanaimobus;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class Route1TimesCcMonfriAActivity extends Activity implements            

OnMenuItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route1_times_cc_monfri_a);
}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
    return true;
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // TODO Auto-generated method stub
    return false;
}
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Derek
  • 632
  • 2
  • 11
  • 29

2 Answers2

1

activity_main.xml in menu folder

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@+id/next"
      android:title="Next" />
 </menu>

In your activity inflate the menu

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

switch (item.getItemId()) //get the id which is an int
{
case R.id.next:  // check if its the menu item next selected
    // Single menu item is selected do something
    // Ex: launching new activity/screen or show alert message
    Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
    startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
    break;

default:
    return super.onOptionsItemSelected(item);   
}
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1
int id = item.getItemId(); 
  switch (id) {
    case R.id.your_menu_item_id : {
       startActivity(new Intent(start_activity, next_activity));

 }

where start activity is your main activity and next_activity is the activity you want to start. Give a look on my blog here for more info.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
  • Thanks! I've since fixed the problem and my app is doing really well these forums are amazing. – Derek May 12 '13 at 06:02