Reputation: 881
I'm using ActionBarCompat to make an actionbar for devices under API 11. It works great and was easy to setup, but I'm stuck.
I have some items on the Actionbar, and it looks great. Some Items are behind the three dots (ifRoom) and some you always can see.
How do I make so when you click on one of these items so it starts an new Activity? I tried with switch/case and other methods, but didnt work to send from one Activity to another through the Items. I know how to send from button, imagebutton to another Activity, but not from Items.
My main.xml looks like this:
<item
android:id="@+id/add"
android:title="Lägg till"
android:icon="@drawable/new"
android:orderInCategory="1"
budsnabben:showAsAction="always"/>
And the code in MainActivity looks like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.:
Intent intent = new Intent(this, MapActivity.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
The problem is on case R.id.... After Id, I dont get my class map or main, its not there.
Thank you.
SOLUTION:
Just want to thank you Gerard.
I created new Strings in strings.xml. After that, I did change the title in my main.xml to this:
android:title="@string/add"
I did hard-coded that line like before, therefore it didn't work I think:
android:title="@+id/add"
Thank you once more.
Upvotes: 2
Views: 3531
Reputation: 2860
Use public boolean onOptionsItemSelected(MenuItem item) { ... }
in your activity using the actionbar and create a switch-case matching item.getItemId()
with the IDs from the menu layout. After that creating the appropriate intent like you would on a regular button.
Upvotes: 2