This question already has answers here:
Closed 10 years ago.
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;
}
}
Use code like below:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Item1");
menu.add("Item2");
return true;
}
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
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.
Related
I am following a tutorial 11. Exercise: Using the contextual action mode
But I am having this error :
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
Error: The method startActionMode(ActionMode.Callback) in the type Activity is not applicable for the arguments (ActionMode.Callback)
I checked this stackoverflow answer
they said to add
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
I had this error
The method getActivity() is undefined for the type Display
what I am doing wrong ? the below is my code.
package com.example.sqlfirst;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Display extends ActionBarActivity {
private final static String TAG = "MainActivity";
protected Object mActionMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_main);
//have to use getSupportActionBar from android.support.v7.app
// ActionBar actionBar = getSupportActionBar();
//getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
View view = findViewById(R.id.gridview);
view.setOnLongClickListener(new View.OnLongClickListener() {
// called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// start the CAB using the ActionMode.Callback defined above
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.ic_action_person:
Toast.makeText(this, "Create a new account please", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Register.class);
startActivity(intent);
return true;
case R.id.ic_action_search:
Toast.makeText(this, "Search for new images", Toast.LENGTH_SHORT).show();
Intent isearch= new Intent(this,Search.class);
startActivity(isearch);
return true;
case R.id.ic_action_picture:
Toast.makeText(this, "Search for new photos", Toast.LENGTH_SHORT).show();
Intent iphotos= new Intent(this,Display.class);
startActivity(iphotos);
return true;
}
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
// called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_action_picture:
Toast.makeText(Display.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
Your Display class is extending ActionBarActivity, that means that it´s an Activity so there´s no need to use getActivity(), you can directly make use of the methods like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* this method is available within your ActionBarActivity*/
startSupportActionMode(modeCallBack);
setContentView(R.layout.grid_main);
// The rest of your code comes here
}
I am a beginner to Android. Trying to add a search a widget in the action bar.
Here is my menu file code ("main_acitivity.xml"):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
My MainActivity class code is:
package com.example.fragment;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.fragment.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.main_activity,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent= new Intent(this,DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I have tried "android.widget.SearchView" as well but it is not even showing the icon. Could you please advise what I am doing wrong ?
you missed these:
xmlns:yourappname="http://schemas.android.com/apk/res-auto"
and
yourappname:showAsAction="always"
yourappname:actionViewClass="android.support.v7.widget.SearchView"/>
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;
}
}
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.
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);
}
}
I did this menu and when I click a menu item nothing happens, it doesnt't even shows the default toast! Maan I dont know what is going on!
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuLapiz" android:alphabeticShortcut="l"
android:icon="#drawable/pencil" />
<item android:id="#+id/menuCirculo" android:alphabeticShortcut="c"
android:icon="#drawable/circulo" />
<item android:id="#+id/menuTriangulo" android:alphabeticShortcut="t"
android:icon="#drawable/triangulo" />
<item android:id="#+id/menuCuadrado" android:alphabeticShortcut="s"
android:icon="#drawable/cuadrado" /> </menu>
DrawActivity.java
... Here I got the menu onOptionsSelected which appereantly is not working
import android.app.Activity
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast; `
public class DrawActivity extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Title
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuLapiz:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
default:
Toast.makeText(this, "This is a toast", Toast.LENGTH_LONG);
return true;
}
}}
I have no idea why the onOptionsItemSelected(MenuItem item) method is not working I already tried without the S of Options because I read that suggestion in other post but it did not work.
I'm new to android programming so I really have no clue of what is going on .. any help will be appreciated, thanks a lot!! pd: let me know if you want to see the DrawView.java class
Add a return true; to the end of each case
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mAdd:
startActivity(new Intent("com.example.ADD"));
return true;
case R.id.mAbout:
startActivity(new Intent("com.example.ABOUT"));
return true;
}
return false;
}
EDIT: toast code
Toast t = Toast.makeText(getApplicationContext(),
"This is a toast",
Toast.LENGTH_SHORT);
t.show();
I need to show menu at the time of button Click.But I am not able to display the menu . My code is below. Can anyone tell me what is wrong in my code ??? Thanks in Advance !!!
Code :
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class DynamicMenu extends Activity {
/** Called when the activity is first created. */
private Context context;
Button btnMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
LinearLayout llay = new LinearLayout(context);
btnMenu = new Button(context);
btnMenu.setText("Show Menu");
llay.addView(btnMenu);
setContentView(llay);
registerForContextMenu(btnMenu);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
Menu m_menu = menu;
m_menu.add(0, 1, 0, "Settings");
m_menu.add(0, 2, 0, "About");
m_menu.add(0, 3, 0, "Exit");
}
}
What do you want to call ? Menu or Context Menu these are two diffrent things.
What you have coded causes a Context Menu to appear. (Long click the button to show the context menu) , heres a sample:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MenuDemo extends Activity {
/** Called when the activity is first created. */
private Context context;
Button btnMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
LinearLayout llay = new LinearLayout(context);
btnMenu = new Button(context);
btnMenu.setText("Show Menu");
llay.addView(btnMenu);
setContentView(llay);
registerForContextMenu(btnMenu);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {//Context Menu that appears when long clicked.
Menu m_menu = menu;
m_menu.add(Menu.NONE, Menu.FIRST+1, 0, "Settings");
m_menu.add(Menu.NONE, Menu.FIRST+2, 0, "About");
m_menu.add(Menu.NONE, Menu.FIRST+3, 0, "Exit");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Menu that appears when menu button is pressed on device
Menu m_menu = menu;
m_menu.add(Menu.NONE, Menu.FIRST+3, 0, "Settings");
m_menu.add(Menu.NONE, Menu.FIRST+4, 0, "About");
m_menu.add(Menu.NONE, Menu.FIRST+5, 0, "Exit");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
String msg="Selected from menu: ";
switch (item.getItemId()){
case Menu.FIRST+3:
Toast.makeText(this, msg+"Settings Menu", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST+4:
Toast.makeText(this, msg+"About Menu", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST+5:
Toast.makeText(this, msg+"Exit Menu", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
String msg="Selected from context menu: ";
switch (item.getItemId()){
case Menu.FIRST+1:
Toast.makeText(this, msg+"Settings", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST+2:
Toast.makeText(this, msg+"About", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST+3:
Toast.makeText(this, msg+"Exit", Toast.LENGTH_LONG).show();
return true;
}
return super.onContextItemSelected(item);
}
}
This is not the best practiced and recommended code you can really make use of polymorphism here. But I hope this gives you an idea.
When i use the following code i can able to get the menu at the time of button click
registerForContextMenu(btnMenu);
btnMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((DynamicMenu) context).openContextMenu(btnMenu);
}
});
Thanks to all.
In the onCreateContextMenu you are not calling the super class constructor. You can refer that from http://developer.android.com/guide/topics/ui/menus.html
Something like this super.onCreateContextMenu(menu, v, menuInfo);