I'm am using this to hide the action-/titlebar in my android application
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
I created my options menu with this:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bottom_menu, menu);
return(super.onCreateOptionsMenu(menu));
And this is the bottom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/refresh"
android:title="Neu laden" />
<item android:id="#+id/settings"
android:title="Einstellungen" />
<item android:id="#+id/logout"
android:title="Ausloggen" />
</menu>
When I press the menu button, the app crashes. When I don't hide the action-/titlebar, everything works fine.
I already found this question on SO, but I don't solve the Problem: Hide Actionbar, Show Options Menu
I found the problem myself, stupid...
I changed:
public class MainActivity extends ActionBarActivity {
to
public class MainActivity extends Activity {
Related
My problem is with menu item icon . I can't see any icon in action bar . There is simply just one activity in my app . In fact it's a simple hello word and i want add an icon to action bar
menu.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=".MainActivity"
>
<!-- black icon -->
<item
android:id="#+id/action_create_order"
android:title="create order"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_add_black_36dp"
>
</item>
<!-- white icon -->
<item
android:id="#+id/action_setting"
android:title="settins"
app:showAsAction="never"
android:orderInCategory="100"
>
</item>
</menu>
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu);
return super.onCreateOptionsMenu(menu);
}
}
why icon doesn't appear in action bar ?
try by changing
app:showAsAction="ifRoom"
to
app:showAsAction="always"
EDIT:
I suggest you change your Activity to AppCompatActivity and also use a Toolbar as your ActionBar
If you use extends Activity you have to use android:showAsAction inside menu.xml
If you want to use app:showAsAction, you have to extend AppCompatActivity as Adolfo Lozano Mendez suggested
Maybe there is no enough place to show icon. Try to rotate the screen (phone/emulator).
I have a menu icon for my ActionBar, but it always goes into the overflow menu no matter what I do. I want it to show up as an icon and not go into the overflow menu.
Here is my menu_daily_selfie.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">
<item android:id="#+id/camera_button"
android:icon="#android:drawable/ic_menu_camera"
android:title="Camera"
app:showAsAction="always"/>
</menu>
I'm using the appcompat library. What gives? Seems like this should work.
Here is my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
My main activity extends ListActivity since it uses a ListView
Then you are not using appcompat-v7 properly. Either:
Switch to inheriting from AppCompatActivity, and manage your own ListView, or
Stop using appcompat-v7, and switch your menu resource to use android:showAsAction instead of app:showAsAction
I created app from "Empty Activity" template in Android Studio 1.4 and added options menu.
Resource:
<?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">
<item
android:id="#+id/action_foo"
android:icon="#android:drawable/ic_media_previous"
android:orderInCategory="100"
android:title="#string/word_foo"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_bar"
android:icon="#android:drawable/ic_media_next"
android:orderInCategory="101"
android:title="#string/word_bar"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_baz"
android:icon="#android:drawable/ic_media_pause"
android:orderInCategory="102"
android:title="#string/word_baz"
app:showAsAction="never"/>
</menu>
Method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
After change the orientation app crashes. What is wrong?
Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.support.v7.widget.Toolbar$SavedState
I readed the answer here class cast exception on orientation change (Android)
and I think it is not my case: I have absolutely empty layout. There is no widgets with id at all.
I found the problem.
I'm not native English speaker, and I forgot, that bar from sequence foo, bar, baz also has common value. And after menu item with id action_foo I created action_bar. It conflicts with some id in SDK layouts, as I presume, corresponded to ActionBar.
So solution is change id in android:id="#+id/action_bar" to something else.
I have exactly the same problem as in these threads:
No actions or icons showing on the actionbar; only in overflow
Menu items won't show up in the actiobar
No one has offered a solution, so I want to bring it up again.
I have a xml-file, my_menu.xml, that look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/delete"
android:showAsAction="ifRoom|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
</menu>
My activity extends ActionBarActivity and implements these methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.delete:
//delete something
break;
}
return(true);
}
In my book: "A value of ifRoom means that the menu item will appear in the action bar if there is space for it" - I have tested to use ifRoom, ifRoom|withText and always, but the item always ends up in the overflow menu. I have tested the application on different devices, and it's plenty of room. Do anyone have a solution to this?
Hank
My activity extends ActionBarActivity
As is covered in the documentation, if you are using the appcompat_v7 backport of the action bar, and its associated ActionBarActivity, your menu resource needs to have showAsAction in a namespace custom to your app:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/delete"
yourapp:showAsAction="ifRoom|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
</menu>
Here is a complete sample project demonstrating the use of the appcompat_v7 action bar.
Try changing it to
<item
android:id="#+id/delete"
android:showAsAction="always|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
Hope this will help you.
Please..please help!
I'm new with android development. After i create an app with simple menu. i click on the parent menu, i don't see the items in the submenu appeared. I don't know why is that. Enyone know this, please teach me.
i use nexus Emulator to show result. (nesux S (4.0", 480 X 800:ddpi)
create menu in the Menu folder -> menu-demo like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/addnew" android:title="add new">
<menu>
<item android:id="#+id/name" android:title="add name"></item>
<item android:id="#+id/age" android:title="Add age"></item>
</menu>
</item>
<item android:id="#+id/update" android:title="update"></item>
<item android:id="#+id/delete" android:title="delete"></item>
</menu>
i update Mainactivity.java in the src.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo, menu);
return true;
}
You're missing android:showAsAction="[property".
You can use a few parameters for showAsAction, such as always, never, and ifRoom.
Add these properties to your code, so for example, a menu item that I always want to appear on the actionbar would be like
<item
android:id="#+id/test"
android:showAsAction="always"
android:title="test" >
</item
For submenus, you basically want the topmost selection shown as an action always and the submenu shown never.