actionbar and wrong button pressed - android

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.

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);

Action Bar items not working when clicked

I'm having an unusual issue here. My action bar was working properly then i went an tested it now and it totally stop. When pressed, they give no response. One is a back button and the other is a send button. Both of which aren't working. Here is my code for the Menu
ActivityOne.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed();
return true;
case R.id.action_send:
new PostUpLoad().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_send.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityOne">
<item
android:id="#+id/action_send"
android:orderInCategory="100"
android:title="#string/send"
android:icon="#drawable/ic_send"
app:showAsAction="ifRoom" />
</menu>
Everything seems fine but they aren't working at all. Any help would be greatly appreciated.
Replace R.id.home with android.R.id.home.
You're missing breaks in your switch case, and I'm not sure about returning true in onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_send, menu);
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
super.onBackPressed(); //might use finish() instead
break;
case R.id.action_send:
new PostUpLoad().execute();
break;
default:
super.onOptionsItemSelected(item);
}
}

How can I create android menu

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

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