How to creat a menu with icons? - android

I really need some guide or suggestion about how to creat a menu with icons.
I am write an Android app, and I want to creat a menu like this, when I enter this app, I can see the "Main" menu in the center, I can edit the text of the menu.I press the icon of main menu, a "+" icon slide out, then I can add a sub menu with press the "+". And next time, I enter this app, I can see the main menu icon around with some sub menu icons. And if I want, I can press main menu icon to hide the sub menu icon.(I cannot post a image, so I hope you can understand)
Really need help about this.

Use this code to make menu in android.And also download these icons from Androidâ„¢ Drawables
res/layout/menu.xml
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/icon_preferences"
android:title="Preferences" />
AndroidMenusActivity.java
package com.androidhive.androidmenus;
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);
}
}
}

Related

Android options menu adds additional items with res/menu-v11/items.xml

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

Icons do not appear in menu android

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

Fourth level menu option not working

I am using a fourth level Active Menu on my app, everything works perfect but when I select one item on the Menu fourth level, the system is not calling onOptionsItemSelected anymore, so I can't get the selected item on my app.
Here is my code
EDIT 1: the dash - in the Id's are making confusion, I am removing them from the code. Sorry for that
public class Main extends FragmentActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .... some code
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Level1: {
Toast.makeText(getBaseContext(), "Level 1", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level2: {
Toast.makeText(getBaseContext(), "Level 2", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level3: {
Toast.makeText(getBaseContext(), "Level 3", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level4: {
Toast.makeText(getBaseContext(), "Level 4", Toast.LENGTH_SHORT).show();
break;
}}
return true;
}
and here the XML file main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Level1"
android:showAsAction="ifRoom"
android:title="#string/Level1">
<menu>
<item android:id="#+id/Level2"
android:title="#string/Level2">
<menu>
<item android:id="#+id/Level3"
android:title="#string/Level3">
<menu>
<item android:id="#+id/Level4"
android:title="#string/Level4"/>
</menu>
</item>
</menu>
</item>
</menu>
</item>
</menu>
I was reading the documentation but it doesn't states a limit for nested sub-menus.
Finally my solution was to append a android:onClick="onOptionsItemSelected" to the fourth level item like:
<menu>
<item android:id="#+id/Level4"
android:onClick="onOptionsItemSelected"
android:title="#string/Level4"/>
</menu>
to force the call to onOptionItemSelected, it works but is there a better solution???
EDIT 2: I just found that this walk-around doesn't works with Android version 4.0.3 or earlier.!!! not even using onMenuItemSelected...!!!
Now I am in problem, please help...!!

Android Menu options in layout does not show icon

I just added a Menu to my activity. Additionally I want to set some icons to each menu item. Therefore I tried both these methods but I do not see these icons on the device. What am I doing wrong ?
I tried the XML way:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:icon="#drawable/help"
android:title="#string/menu_help" />
<item android:id="#+id/settings"
android:icon="#drawable/settings"
android:title="#string/menu_settings" />
<item android:id="#+id/num"
android:icon="#drawable/num_icon"
android:title="#string/menu_num" />
</menu>
Method 2:
In my code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//Set icon for the menu button
Drawable num_icon = getResources().getDrawable(R.drawable.num_icon);
menu.getItem(2).setIcon(num_icon);
Drawable settings_icon = getResources().getDrawable(R.drawable.settings);
menu.getItem(1).setIcon(settings_icon);
Drawable help_icon = getResources().getDrawable(R.drawable.help);
menu.getItem(0).setIcon(help_icon);
return true;
Also, could anyone tell me how big should these icon sizes be? Mine is256x256 pix. Thanks
In the android sdk menu icons are of the following sizes:
ldpi 32 x 32 px
mdpi: 36 x 36 px
hdpi: 48 x 48 px
xhdpi: 64 x 64 px
256x256 is too large.
use following code it may help you..
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/icon_bookmark"
android:title="Bookmark" />
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/icon_preferences"
android:title="Preferences" />
</menu>
Now open your main Activity class file and put following code
MenusActivity.java
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);
}
}
}
You just have need to small changes in your manifest file according to your need you can change theme I get the solution for above problem with small change i.e.even using any version of android it will work for (2.3 to latest version 4.4 kitkat also).
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.WithActionBar">

creating menu in Android

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.

Categories

Resources