I'm currently creating and defining a navigation drawer. I want now a header view, like their on the google apps, above the item rows. I only found examples with RecyclerViews, and i dont want to use it. I have all finished with a ListView and all other stuff. Maybe someone can help me :) Thanks in advance
You can create NavigationView using android design support library without having pain to create listview or RecyclerView, its all created by android.
To add it to your project you need to add the android design support library to your project, add below line in build.gradle
compile 'com.android.support:design:22.2.0
Check out android design support features here
First create a header(header.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nyname"
</RelativeLayout>
Next create a menu resource file, the items in the menu will be the items displayed in the drawer(drawer.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/first1"
android:checked="false"
android:icon="#drawable/icon1"
android:title="#string/string1" />
<item
android:id="#+id/second2"
android:checked="false"
android:icon="#drawable/icon2"
android:title="#string/string2" />
</menu>
Next create a DrawerLayout file, within the drawerlayout you can see I have included a Toolbar and a 'FrameLayout`. When the item in the drawerlayout is clicked you can replace fragment.
Also within it is the NavigationView with these parameters:
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
android:layout_gravity="start"
app:headerLayout is the header.xml that we created in step 1.
app:menu is the menu resource item i.e drawer.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
Next in your MainActivity extend AppcompatActivity,
public class MainActivity extends AppCompatActivity {
............................................
Intialise NavigationView and call setNavigationItemSelectedListener to get click events,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment
case R.id.first1:
SomeFragment fragment = new SomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
...................
Step by step procedure to create navigationview go here
How it would look:
Related
How to handle clicks from a toolbar with Navigation Component in android?
I have a MaterialToolbar in an activity with a menu in xml, the menu shows up and accepts clicks but no action is executed.
This is how I initialize the navigation component:
binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
navController = Navigation.findNavController(this, R.id.fragment_container);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(binding.activityHome)
.build();
NavigationUI.setupWithNavController(binding.navigationView, navController);
NavigationUI.setupWithNavController(binding.toolBar, navController, appBarConfiguration);
This is my Toolbar menu, it has only one item that will open a DialogFragment:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/dialog_account"
android:icon="#drawable/icon_account"
android:title="#string/dialog_account"
app:showAsAction="always" />
</menu>
Then this is the dialog in the navigation_graph
<dialog
android:id="#+id/dialog_account"
android:name="my.package.name.dialogs.DialogAccount"
android:label="#string/dialog_account"
tools:layout="#layout/dialog_account" />
This is the activity layout, nothing fancy there:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data></data>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="#+id/fragment_container"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/tool_bar"
style="#style/toolbar_style"
app:menu="#menu/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="#dimen/null_dimen"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
style="#style/navigation_view" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
The Activity layout has a NavigationView and a MaterialToolbar, the navigation from the NavigationView works fine, but I can't find a way to open my dialog from the Toolbar even though it seems like is taking clicks but doing nothing. I have tried with onCreateOptionsMenu but it does not get triggered, guess the Navigation Component is working but my dialog is not opening on touching of the menu.
What am I missing here, any hand?
To handle on click on Toolbar simply use "setOnMenuItemClickListener" by providing id of the toolbar.
mToolbar= findViewById(R.id.main_tollbar);
mToolbar.inflateMenu(R.menu.game_menu);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.search_menu:
Toast.makeText(MainActivity.this,"Search Successful",Toast.LENGTH_LONG).show();
break;
case R.id.menus_logout:
Toast.makeText(MainActivity.this,"Logout Successful",Toast.LENGTH_LONG).show();
break;
case R.id.acc_settings:
Toast.makeText(MainActivity.this,"Setting Successful",Toast.LENGTH_LONG).show();
break;
case R.id.about_menu:
Toast.makeText(MainActivity.this,"About Successful",Toast.LENGTH_LONG).show();
break;
case R.id.feedback_menu:
Toast.makeText(MainActivity.this,"Feedback Successful",Toast.LENGTH_LONG).show();
break;
}
return true;
}
});
If you are using the Toolbar (without using setSupportActionBar(toolbar);) you have to use:
NavigationUI.setupWithNavController(toolbar,navController,mAppBarConfiguration);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
NavController navController = Navigation.findNavController(context, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController);
}
});
In your case it is enough since the id in the menu matches with the id in the navigation graph.
In general you can also use:
public boolean onMenuItemClick(MenuItem item) {
Navigation.findNavController(view).navigate(R.id.action...);
//....
}
Final note: the method onCreateOptionsMenu is only triggered if your are using an ActionBar.
For Navigation component you can use inflator
val view-inflater.inflate(R.Layout.fragment_main, container, false)
view.textview1.setonClickListener{
Navigation.findNavController(view).navigate(R.id.settings)
}
I do have placed the navigation drawer icon on the action bar and it certainly is showing up, but upon click on the drawer icon the function is not happening, only upon swipe the navigation drawer is opening.
ArticleActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mProgressView = findViewById(R.id.progress);
mFullContainerView = findViewById(R.id.full_container);
ActionBar actionBar = getActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
activity_article.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_article_drawer" />
<include layout="#layout/list_view" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
From your code it looks like you are using your custom toolbar as actionbar, But you have not set it as support actionbar. You need to use setSupportActionBar(toolbar) to set your custom toolbar as actionbar.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Also, make sure you are extending your application main theme from theme with no actionbar.
I am trying to toggle the navigation view by the hamburger icon. The navigation view is appearing on sliding from the left. The following is the code that I have included in my MainActivity "onCreate Function"
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
getSupportActionBar().setHomeButtonEnabled(true);
toggle.syncState();
I also have tried this code block which doesn't seem to work.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I also tried this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
My main_activity.xml is this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
>
<RelativeLayout
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"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/app_bar_main"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/bottom_navigation_bar"
app:itemTextColor="#drawable/bottom_navigation_bar"
app:menu="#menu/my_navigation_items" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentTop="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is my app_bar_main.xml which is where I define the ui of toolbar.
enter code here
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.moodindigo.MainActivity">
<!--<RelativeLayout-->
<!--android:id="#+id/relativelayout_for_fragment"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--></RelativeLayout>-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="Mood Indigo"
/>
</android.support.design.widget.AppBarLayout>
<!--<include layout="#layout/content_main" />-->
</android.support.design.widget.CoordinatorLayout>
Any help would be highly appreciated. I don't know what I am doing wrong. Please let me know if anything else is needed. I didn't paste the whole code here as the project is quite big. I checked many StackOverflow post for this and I tried to do the same thing but it doesn't seem to work.
try this in the onCreate of your main activity :
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
i hope it will help you!
In your ActionBarDrawerToggle constructor you use toolbar as third parameter. I am not sure if it is valid, since the official documentation for ActionBarDrawerToggle constructor parameters quotes:
Parameters:
activity - Activity: the Activity hosting the drawer
drawerLayout - Drawerlayout: the DrawerLayout to link to the given Activity's
ActionBar
drawerImageRes - int: A Drawable resource to use as the drawer
indicator
openDrawerContentDescRes - int: A String resource to describe the
"open drawer" action for accessibility
closeDrawerContentDescRes - int: A String resource to describe the
"close drawer" action for accessibility
So, the third parameter should be Drawable Resource but toolbar is a View.
Edit:
As you are using v7 and not v4, there is one sentence from official documentation https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html I think is important:
Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity.
So it seems you should use differnt constructor from the one you are using now.
Hope this will help.
I have created an entire project on a blank activity(Android Studio). I want to add a navigation drawer without hampering with my previous code.Is there a way to add a navigation drawer to a blank activity, if yes please do guide.
Step1:Include navigation view to your xml file which will be inflated by yor activity on create.
<?xml version="1.0" encoding="utf-8"?>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
step2:initialize views
private NavigationView navigationView;
private DrawerLayout drawer;
step3: sent the event on drawer close and open
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
hope this helps how set the navigation drawer.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/icon">
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/lvLeft"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ff8800"
android:choiceMode="singleChoice"
android:divider="#android:color/holo_blue_bright"
android:dividerHeight="0dp" />
<ListView
android:id="#+id/lvRight"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#0077ff"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
I tried setting the menu items in my app as actions with app:showAsAction="always". This works on a phone and also on the tablet when I use the same layout.
But when I use a two-pane layout, the items don't show up in the action bar but in the overflow menu even though there is plenty of room.
Here is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_item_maps"
android:icon="#drawable/ic_action_place"
android:orderInCategory="20"
android:title="#string/maps_menu_item"
app:showAsAction="always">
</item>
<item
android:id="#+id/menu_item_event"
android:icon="#drawable/ic_action_event"
android:orderInCategory="30"
android:title="#string/menu_item_event"
app:showAsAction="always">
</item>
<item
android:id="#+id/menu_item_share_gig"
android:orderInCategory="100"
android:title="#string/menu_item_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always">
</item>
</menu>
Here is the two-pane layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
>
<fragment
android:id="#+id/gig_list"
android:name="de.nobodyknows.app.GigListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="#+id/gig_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
In my code I use the callback method to replace the FrameLayout with a detail fragment:
/**
* Callback method from {#link GigListFragment.Callbacks} indicating that
* the item with the given ID was selected.
*/
#Override
public void onItemSelected(Long id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putLong(GigDetailFragment.GIG_ID, id);
GigDetailFragment fragment = new GigDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.gig_detail_container, fragment).commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, GigDetailActivity.class);
detailIntent.putExtra(GigDetailFragment.GIG_ID, id);
startActivity(detailIntent);
}
}
The detail Fragment inflates the options menu that was shown above:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail, menu);
}
Now these menu items show up fine in the phone layout when a new acticity is started. But when the fragment replaces the FrameLayout in the first activity, the menu items just show up in the overflow menu. I can't figure out why that happens, though. Has it something to do with the support library? Or is it something special about how fragments work?
Thanks for your help.
Okay, I found the solution.
My Activity did not extend theActionBarActivityand therefore was not correctly set up to work with MenuItemCompat.
The reason why it still worked on the phone was because that was in a different Activity that correctly extended ActionBarActivity.
To correct this I just replaced the superclass of the Activity that acts as the two-pane Activity to be ActionBarActivity.