package com.droidnova.android.howto.optionmenu;
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 SimpleOptionMenuActivity 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.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
break;
case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
i'm trying to create this simple menu, it loads up in the emulator (1.6 version ) but none of the submenues show up
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/icon"
android:icon="#drawable/icon" />
<item android:id="#+id/text"
android:title="Text" />
<item android:id="#+id/icontext"
android:title="Icon and text"
android:icon="#drawable/icon" />
thats the xml file
I think you need to create another menu element inside your main menu element in your xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu>
<item android:id="#+id/icon"
android:icon="#drawable/icon" />
<item android:id="#+id/text"
android:title="Text" />
<item android:id="#+id/icontext"
android:title="Icon and text"
android:icon="#drawable/icon" />
</menu>
There's an easy tutorial for submenus here.
Related
I am just starting learning Android and have some troubles with creating menu in app.
I tried all options to create menu but no one work for me.
When I run emulator or real device menu doesn't appear.
I have tried "Ctrl+M" and different devices but it doesn't work.
What is the problem?
My MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionMenu(final Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
my activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.genaepic.p013_contextmenu.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just do it!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
my main_menu.xml
<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="#string/item1"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings2"
android:orderInCategory="100"
android:title="#string/item2"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings3"
android:orderInCategory="100"
android:title="#string/item3"
app:showAsAction="never" />
<item
android:id="#+id/action_settings4"
android:checkable="false"
android:orderInCategory="100"
android:title="#string/item4"
app:showAsAction="always" />
enter image description here
The problem is due to the showAsAction tag. Your item that inflates your overflow menu MUST BE showAsAction="always", otherwise menu may hide in devices. An example snippet:
<item
android:id="#+id/settings"
android:icon="#drawable/my_drawable"
app:showAsAction="always"
android:title="Setting">
</item>
EDIT
You can override the onCreatOption to inflate the menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu: ");
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected: ");
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
This is my Java code. myFrag class extends FragmentActivity and I have used switch case to select each item in the menu. All the other menu items show without a problem and every time the menu actionflow button is clicked additional items named res/menu-v11/items.xml are automatically added.
Please also see the screen shot of how the menu items appear.Thanks in advance.
package com.biasharafinder.bidhaa;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class myFrag extends FragmentActivity {
#Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
menu.add(R.menu.items);
return super.onPrepareOptionsPanel(view, menu);
}
//OPTIONS MENU STARTS HERE
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
menu.clear();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.search:
Intent startSearch=new Intent(myFrag.this,MainActivity.class);
startActivity(startSearch);
break;
case R.id.login:
// login.show();
break;
case R.id.settings:
// Toast.makeText(getBaseContext(), "You selected settings",
break;
case R.id.exit:
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
The switch case is used to select between items
Here is my items xml code in the menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:title="#string/search_"
android:icon="#drawable/search"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/publish"
android:title="#string/publish_"
android:icon="#drawable/publish"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/login"
android:title="#string/login_"
android:icon="#drawable/login"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/settings"
android:title="#string/settings_"
android:icon="#drawable/settings"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/exit"
android:title="#string/exit_"
android:icon="#drawable/power"
android:showAsAction="ifRoom|withText"
/>
</menu>
I think you should add menu(not in menu-v11)
Select res folder, right click on it
New > Android Resource Directory > Resource Type = menu > Press Enter
There you are. Now you will see menu folder under res.
To add menu's xml inside menu
New > Menu Resource File > Name the file > Press Enter
it should be like this res/menu/items.xml
How can I replicate something like I made below in Balsamiq?
I made this menu, but it is only displaying the text of the items (not the icons). Is it possible to display both the title and icon in a PopupMenu?
Here is my create_post_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_photo"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_photo"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_video"
android:icon="#drawable/ic_action_video"
android:title="#string/action_video"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_text"
android:icon="#drawable/ic_action_edit"
android:title="#string/action_text"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_link"
android:icon="#drawable/ic_action_web_site"
android:title="#string/action_link"
android:showAsAction="always|withText" />
</menu>
Edit
Here are my onCreateOptionsMenu and onOptionsItemSelected methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_new) {
View menuItemView = findViewById(R.id.action_new);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.create_post_menu);
popupMenu.show();
return true;
} else if(item.getItemId() == R.id.action_search) {
return true;
} else if(item.getItemId() == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
} else if(item.getItemId() == R.id.action_help) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
I resolved this issue by simply putting the create_post_menu inside of the item whose icon is a +.
For example, I have (using AppCompat):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action_photo"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_photo"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_video"
android:icon="#drawable/ic_action_video"
android:title="#string/action_video"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_text"
android:icon="#drawable/ic_action_text"
android:title="#string/action_text"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_place"
android:icon="#drawable/ic_action_place"
android:title="#string/action_place"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_more"
android:title="#string/action_more"
android:visible="false"
app:showAsAction="always|withText" />
</menu>
</item>
...(more menu items here)
</menu>
Without AppCompat, you could just get rid of the XML Namespace app by replacing app with android.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);//your created butto
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
}
I hope my previous answer Here can help you.
If you just want a similar popup menu, you can use ActionProvider. It's more powerful.
If you want it as a true menu, you can use custom PopupMenu.
In my code there is a menu in which i am using icons. but while running only the icon title appears not the icon. please help me to solve this.In my code there is a menu in which i am using icons. but while running only the icon title appears not the icon. please help me to solve this
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/menu_icon"
android:title="Bookmark" />
<item android:id="#+id/menu_save"
android:icon="#drawable/menu_icon1"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/menu_icon2"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/menu_icon3"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/menu_icon4"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/menu_icon5"
android:title="Preferences" />
</menu>
AndroidMenusActivity.java
package example.menuexmp;
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 AndroidMenusActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// 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);
}
}
}
Hiii Check this links..
and tell us on which Version are you testing.
You should Check your API versions and Code as per the requirment,
Here
Here
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.