Please help me how can i call a menu resource in other activity .
Here is code of main activity
public class ControlMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, ShowSettings.class);
startActivity(intent);
break;
case R.id.services: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.another:
}
return true;
}
here is menu resource
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Quit"
android:title="Quit"
android:icon="#drawable/icon" />
<item android:id="#+id/settings"
android:title="Settings"
android:icon="#drawable/icon" />
<item android:id="#+id/services"
android:title="Services"
android:icon="#drawable/icon" />
</menu>
i can call it in the other activity by writing the main activity code but for that i have rewrite the case statements as well so guide me how can i solve it out .
You should be able to add the menu code to a common Activity subclass, then make all your other Activity classes extend that common class instead of just Activity. For a simple menu this should work just fine.
Related
I am running this code on my android phone and i am unable to see menu items on my android phone but when i run this on emulator, i can see menu items.
Please help . Here is the screenShot of.
<item android:title="About Us"
android:id="#+id/aboutUs_ID"/>
<item android:title="Preferences"
android:id="#+id/preferences_ID"/>
java code in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu_file, menu);
return true;
}
Try to use below solution;
res/menu/main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Color Theam"
app:showAsAction="never" />
<item
android:id="#+id/action_appMahiti"
android:orderInCategory="100"
android:title="#string/tab_refrence"
app:showAsAction="never" />
</menu>
Here is the code snippiest you can add in your activity class
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// perform operation or display message
Toast.makeText(this, "CLick me", Toast.LENGTH_SHORT).show();
break;
case R.id.action_appMahiti:
// perform operation
startActivity(new Intent(this, XYZ.class));
break;
}
return super.onOptionsItemSelected(item);
}
I will suggest to try to use above solution.
I'm implementing action bar with items. I can't see the selected item effect (background selection color) when I click on an item in the actions :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="TTTTTTT"
android:checkable="true"
/>
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="AAAAAA"
android:checkable="true"
/>
</menu>
On My SherlockActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
UPDATE:
You need to do something like this (switching the icons ids):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
-----------
Have you done something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
This is needed to inflate the XML file.
Maybe setting identical id's on both items is causing the problem. Did you try to change one of them?
Well this is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.news:
Intent j = new Intent(MainScreen.this,LatestNewsScreen.class);
startActivity(j);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
this is my manifest lines I added:
<activity android:name=".LatestNewsScreen"
> </activity>
and i the latestNewsScreen I must see an simple textView. I press menu and if I click nothing happens. Why Is that?
this is my menu.xml code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/news"
android:icon="#drawable/icon"
android:title="About us" />
</menu>
Any help?
When you click menu, you should see the menu items (in this case just the one) appear at the bottom of the screen. When you click that item then your other activity should start.
Maybe your icon is not the right size, try changing it to the system one, like
android:icon="#android:drawable/ic_menu_info_details"
and see what happens
I am new to Android application development.I want to develop a simple android application which contains menus.Is there any source code on internet.Can anybody tell me how should i pursue
Thanks in advance
Tushar
Everything you need to know is in the Android Dev Guide.
What it comes down to - and I'm just copying relevant parts from the Android Dev guide - is creating an XML menu resource, e.g. this one, and saving it as game_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
When an item is clicked, you can do several actions:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
XML CODE:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_new"
android:title="New" />
<item android:id="#+id/menu_about"
android:title="About" />
<item android:id="#+id/menu_help"
android:title="Help" />
</menu>
Main Code:
package com.menuexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MenuSample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_about:
Toast.makeText(MenuSample.this, "You Clicked About", 3000).show();
return true;
case R.id.menu_help:
Toast.makeText(MenuSample.this, "You Clicked Help", 3000).show();
return true;
case R.id.menu_new:
Toast.makeText(MenuSample.this, "You Clicked New", 3000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The previous answers have covered the traditional menu used in android. Their is another option you can use if you are looking for an alternative
https://github.com/AnshulBansal/Android-Pulley-Menu
Pulley menu is an alternate to the traditional Menu which allows user to select any option for an activity intuitively. The menu is revealed by dragging the screen downwards and in that gesture user can also select any of the options.
I have defined a menu as laid out by http://developer.android.com/guide/topics/ui/menus.html
Here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_home"
android:icon="#drawable/ic_menu_home"
android:title="Main Menu" />
<item android:id="#+id/menu_signout"
android:icon="#drawable/ic_menu_signout"
android:title="Sign Out" />
</menu>
and my code, placed in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(ManageUsersActivity.this, MainMenuActivity.class));
finish();
return true;
case R.id.menu_signout:
//TODO: issue signout to clear the cookie
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Unfortunately, when I click the menu button, nothing happens.
Try chaining to the superclass in onCreateOptionsMenu(). Here is a sample project demonstrating the use of MenuInflater for options and context menus.