-1

Possible Duplicate:
Android, How to create option Menu

i have tried all the things but i am not able to create option menus.can u please give me full code for that ? should i make any changes in manifest file to create menus ? should i create another class only for menu or i can write it in any class created before ? please help me.can i write like this :

package com.example.FirstProject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

//import android.widget.EditText;
//import android.widget.RadioButton;
//import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;



public class FirstProjectActivity extends Activity {
/** Called when the activity is first created. */
protected ListAdapter adapter;
Cursor cursor;
 protected String[] cities = {"Mumbai"};
 ListView lv ;
 ListView stations;

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


public void myClickHandler(View view)
{
     Intent i=new Intent(this,City.class);
     startActivity(i);

}

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
     {
        if( R.id.icon==item.getItemId())     Toast.makeText(this, "You pressed the     icon!", Toast.LENGTH_LONG).show();

       if( R.id.icon==item.getItemId())      Toast.makeText(this, "You pressed the     text!", Toast.LENGTH_LONG).show();

       if( R.id.icon==item.getItemId())  Toast.makeText(this, "You pressed the      icon and text!", Toast.LENGTH_LONG).show();

    }
    return true;
}



}
Community
  • 1
  • 1
TechHelper
  • 822
  • 10
  • 24

3 Answers3

0

try with this...

public boolean onOptionsItemSelected(MenuItem item)
    {
        System.out.println("entered to option selected");
        switch(item.getItemId())
        {

        case  R.id.icon:
            System.out.println("entered to icon");
            Toast.makeText(this, "icon clicked", Toast.LENGTH_LONG).show();
            break;

        case R.id.text:
            Toast.makeText(this, "text clicked", Toast.LENGTH_LONG).show();
            break;
        case R.id.iconandtext:
            Toast.makeText(this, "icon and text clicked", Toast.LENGTH_LONG).show();
            break;

        }
        return true;

    }

refer this link.. also it will be usefull enter link description here

user1213202
  • 1,305
  • 11
  • 23
0

Use code like below:

 @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
         menu.add("Item1");
         menu.add("Item2");
         return true;
        }
asma
  • 71
  • 2
  • 11
0

You can use the following code inside the menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
  android:id="@+id/menu1"
  android:alphabeticShortcut="s"
  android:title="menu1 title"
  />
  <item
  android:id="@+id/menu2"
  android:alphabeticShortcut="t"
  android:title="menu2 title"
  />
 </menu>

now the two methods for the invoking the menu and function which is to be called on the item selected.

@override
public boolean onCreateOptionsMenu (Menu menu){
    super.onCreateOptionsMenu(menu);
     //menu.add(R.menu.main_menu);
    //menu.add(Menu.NONE, 101,Menu.FIRST,this.getResources().getString(R.string.Title));
     MenuInflater mi = getMenuInflater();
     mi.inflate(R.menu.main_menu, menu);
     return true;
}

you can use the both the method of invoking the menu. First one by the menu.add and second one by the inflating the menu.xml file Now on the itemselectedlistner is used by the following code

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.menu1:
        //  your code here
    case R.id.menu2:
        //   your code here
    }
    return false;
}

just pass the id in switch-case and do whatever you want by selecting the those menu.

Arpit Patel
  • 1,561
  • 13
  • 23