How can I create android menu - android

I want to create menu like this menu
because when I did like in the link it is appear on the top and I want it down as in the link
This is my java File look at to these code and I am waiting for answers please
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.undo:
Toast.makeText(Map.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.track:
Toast.makeText(Map.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.Hyper:
Toast.makeText(Map.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my XML file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/undo"
android:icon="#drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="#+id/track"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/Hyper"
android:icon="#drawable/icon_search"
android:title="Search" />
</menu>

You're looking for Android Action Bar menu itens:
Take a look: http://developer.android.com/guide/topics/ui/actionbar.html

Related

Menu Item Select Problems

This is my NavagatorView code,
<item
android:icon="#drawable/ic_home_black_24dp"
android:title="Main"
android:id="#+id/Main"
/>
<item
android:icon="#drawable/ic_wc_black_24dp"
android:title="Matched"
android:id="#+id/Matched"
/>
This is my click register code,
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.Main:
//newGame();
return true;
case R.id.Matched:
displayInfoDialogView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I want it so when I click "Matched" the displayInfoDialogView(); runs.
Now when I run it nothing happens.
add this code below in your onCreateView method:
setHasOptionsMenu(true);

How to add an option menu item?

I'm totally new in android and I'm doing an app that change the background color when I press a button. I have already done that but now I want to add a menu item which will start the mode with or without buttons.
I was trying to do this but I don't know if I'm doing good.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item){
switch(item.getItemId()) {
case R.id.action_settings:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hidden").
setPositiveButton("OK", null).
create().
show();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
and XML
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Hidden"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Visible"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Cancel"/>
</menu>
First edit menu items id: from android:id="#+id/action_settings" to android:id="#+id/action_hidden"; from android:id="#+id/action_settings" to android:id="#+id/action_visible";
from android:id="#+id/action_exit" to android:id="#+id/action_cancel";
XML file:
<menu>
<item
android:id="#+id/action_hidden"
android:title="Hidden"/>
<item
android:id="#+id/action_visible"
android:title="Visible"/>
<item
android:id="#+id/action_cancel"
android:title="Cancel"/>
</menu>
Then use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater menuInflater = getMenuInflater();
getMenuInflater().inflate(R.menu.YOUR_ACTIVITY!!!!, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_hidden:
//code for action_hidden
return true;
case R.id.action_visible:
//code for action_visible
return true;
case R.id.menu_cancel:
//code for menu_cancel
return true;
default:
return super.onOptionsItemSelected(item);
}
}

actionbar and wrong button pressed

hello i have an actionBar with this menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_open"
android:title=".."
android:icon="#drawable/open_holo_light"
yourapp:showAsAction="always|withText" />
<item android:id="#+id/menu_delete"
android:title=".."
android:icon="#drawable/delete_holo_light"
yourapp:showAsAction="always|withText" />
<item android:id="#+id/menu_detail"
android:title=".."
android:icon="#drawable/info_holo_light"
yourapp:showAsAction="always|withText" />
</menu>
this is when i insert my menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.my_menu, menu);
return true;
}
here i press my menu items:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_open:
Toast.makeText(this, "open", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_detail
Toast.makeText(this, "detail", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
the problem is easy! if i press menu_open i have no toast, if i press menu_delete i have open toast, if i press menu_detail i have delete toast.. why? thanks!
Remove this line:
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
And change every yourapp:showAsAction with android:showAsAction.

How to track the Share button clicked on the ActionBar?

I have added Share option to my ActionBarSherlock, this way:
public boolean onCreateOptionsMenu(final Menu menu) {
menu.add("Share")
.setIcon(R.drawable.ic_title_share_default)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??
You should create an XML file to define menu items.
For example mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/share"
android:icon="#drawable/ic_title_share_default"
android:title="#string/share"
android:showAsAction="ifRoom"/>
</menu>
Then in the onCreateOptionsMenu you will do:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle the item selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
//do something for share
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can see more information in here:
http://developer.android.com/guide/topics/ui/menus.html
http://actionbarsherlock.com/usage.html

when i select menu option once. onOptionsItemSelected is called twice. (Android)

When my menu is clicked two times onoptionitemselected is called. how to stop it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.docmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.upload:
Log.e("testing", "called");
return true;
case R.id.back:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
my menu xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/upload"
android:icon="#drawable/menu_upload"
android:title="#string/upload" />
<item android:id="#+id/back"
android:icon="#drawable/menu_back"
android:title="#string/back" />
</menu>
when upload icon is selected. In log testing called is printed two times.
#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.Aboutus:
final Dialog d1 = new Dialog(Welcome.this);
d1.setContentView(R.layout.aboutus);
d1.show();
break;
And make sure that you have created folder under res named menu. and make new menu.xml file
and put code like this in menu.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/Aboutus"
android:title="About Us" android:icon="#drawable/ic_menu_about_us" />
<item android:id="#+id/Settings"
android:title="Settings" android:icon="#drawable/ic_menu_settings"/>
<item android:id="#+id/help"
android:title="Help" android:icon="#drawable/ic_menu_help" />
onOptionsItemSelected return true is ok
Try this code...
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_LONG).show();
break;
case R.id.my_settings:
Toast.makeText(getApplicationContext(), "Home Page", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Exit", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
And create a new xml in menu folder and apply this code.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/my_settings"
android:title="#string/my_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

Categories

Resources