How to create custom option menu in android - android

I am creating an option menu. it has three option HOME, EMAIL, VISIT. All three options coming at the same row. i have to show HOME option above and rest two below to Home Option(according to attached Screen Shot).
Any Ideas??
Thanks.
Here is my XML file.
<menu android:id="#+id/new_game"
android:icon="#drawable/red_no_bg"
android:title="Home" />
<item android:id="#+id/email"
android:icon="#drawable/red_no_bg"
android:title="Email" />
<item android:id="#+id/help"
android:icon="#drawable/red_no_bg"
android:title="Visit Microsite" />
</menu>
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.overview_menu, menu);
return true;
}
Using the above code i am getting the all option in Same line.

You can Create in two ways
1.Statically.
2.Dynamically.
Am explaining the easiest method of adding menus.
Step 1: Create a new Android Application.
Step 2: After onCreate() method write onCreateOptionMenu();
Step 3: In onCreateOptionMenu() Write these lines
menu.add(11, 21, 1, "Gowtham").setIcon(R.drawable.ic_launcher);
Here "Gowtham" is title of your menu item You can Home&Email use instead of "Gowtham".
Ans use Any images Instead of ic.launcher

Related

How do I get my ActionBar Items to show as icons, and not in the overflow menu?

First off, I'm running the latest Lollipop - Minimum and targeted SDK are both 22
minSdkVersion 22
targetSdkVersion 22
in the gradle build. I've changed/re changed these to 22, as well as rebuilt/invalidated caches/cleaned. I'm extending Activity in my main:
public class login_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
}
#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.login_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
And here are my menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="ifRoom"/>
</menu>
When I run my code, it keeps showing the items in the ActionBar overflow menu. How do I fix this? Thanks!
Try replacing "ifroom" with "always" for showAsAction like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="always"/>
</menu>
I actually spent nearly five hours trying to figure this out, but I actually figured it out just now going through my gradle files. Here's how you fix it:
Go to your Gradle Scripts folder
Open the build.gradle file with the SDK options in it
Remove the line under dependencies (Mine's this one, not sure how they vary):
compile 'com.android.support:appcompat-v7:22.2.0' (REMOVE THIS)
Go back to your menu file and REMOVE the namespace definition for the namespace you put before showAsAction
xmlns:app="http://schemas.android.com/apk/res-auto" (REMOVE THIS)
Go change the showAsAction line from something:showAsAction to android:showAsAction
Rebuild & compile. It should work! I wish changing the SDK cleaned up after itself, but hey, what can you do.

Creating an Options Menu not working for me

Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as
<?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"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and then inflating the menu in the activity as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.
My activity extends FragmentActivity, in case that matters.
The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.
I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.

Animation-list on Menu Item Icon

I want to have an Menu item in the ActionBar that visualizes a boolean value. If this boolean value is true, the icon should start animating a series of 2 images in a loop. I did the following:
I added an item in the menu.xml file:
<item
android:id='#+id/myAnimation'
android:icon='#drawable/pic1'
android:showAsAction='ifRoom'>
</item>
I created an animation-list:
<?xml version='1.0' encoding='utf-8'?>
<animation-list xmlns:android='http://schemas.android.com/apk/res/android'
android:oneshot='false' >
<item
android:drawable='drawable/pic1'
android:duration='100' />
<item
android:drawable='drawable/pic2'
android:duration='100' />
</animation-list>
In the method onCreateOptionsMenu I assign this menu item to a variable
myMenuItem = menu.findItem(R.id.myAnimation);
Now, I was hoping to add an animation which would get executed whenever I tell the menu item to animate. But I wasn't able to come even close.
is this even possible?
How can I do that?
Thanks! Any help is appreciated!
Edit:
The App should support API from 8
I did ((AnimationDrawable) item.getIcon()).start(); in onCreateOptionsMenu(Menu menu)
This did not work. Then I thought, maybe it should be done on the UI thread, and then this worked:
someView.postDelayed(new Runnable() {
#Override
public void run() {
((AnimationDrawable) item.getIcon()).start();
}
}, 1000);
I was looking for the same thing and just found your question unanswered, so here is the solution, from the Android dev website:
((AnimationDrawable)myMenuItem).start();

Android: Emulator won't open menu in app

I'm having a rather odd issue where the menu for an activity works totally fine on real device but not on the emulator.
I tried launching one of my older projects in the emulator that I remember the menus working in, and it also failed. I suppose some sort of update has caused this?
The code is simple...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem prefs = menu.add("Preferences");
prefs.setIcon(R.drawable.gear_01);
return true;
}
LogCat displays series of exceptions ultimately caused by:
E/AndroidRuntime(6714): Caused by: java.io.FileNotFoundException: res/drawable-hdpi/ic_menu_more.png
This actually happens not only in my applications but on the home screen to.
Any ideas for solutions?
Its hard to find error in your code.
So try this.Create one Folder in res named menu.Put this xml inside this folder
Make Sure you have icon in your drawable names ic_new_game,ic_help
<?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"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Override this method as you did
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}

Android:Option menu appearance and icon not getting displayed

I am facing a few problems with the android option menu.
Here is my code:
Inside res folder, i created a menu folder containing menu.xml file with this below code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#drawable/green"
android:id="#+id/icon" />
<item
android:id="#+id/text"
android:title="Text"/>
<item
android:id="#+id/icon1"
android:title="Icon and Text"
android:icon="#drawable/icon"/>
</menu>
And this is my SimpleOptionMenuActivity.java file:
public class SimpleOptionMenuActivity extends Activity {
#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, "Icon menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.text:
Toast.makeText(this, "Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.icon1:
Toast.makeText(this, "Icon and Text menu option is clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
1) On clicking the menu button of emulator, the menu is getting displayed like a ListView i.e. one above another, but i want the options to appear one beside another, 3 items per row, i.e. if there are four items then there will be two rows (which is usual, i don't know how to explain this properly).
2) The image is not getting displayed. I checked the R.java file, the icon is present inside the drawable inner class but still it's not getting displayed in the menu.
Please help me to solve the two problems.
Change the API version from 15 to 11 or lower . Also , the image is not getting displayed because you might have selected an image too large for a certain drawable folder .
drawable-hdpi with 72 by 72 pixel icons
drawable-mdpi with 48 by 48 pixel icons
and
drawable-ldpi with 36 by 36 pixel icons
Question 1:
in the AndroidManifest.xml, find something like the following:
"
uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"
"
remove the android:targetSdkVersion="16"
Question 2:
my menu.xml file like this:
<item android:id="#+id/about"
android:icon="#drawable/about"
android:title="#string/about"
android:showAsAction="ifRoom|withText" />
it works, show the image icon, you can try it.

Categories

Resources