I have one action (MainActivity) that extends actionbaractivity and implements NavigationDrawer. I installed drawertoggle in the activity itself.
I create new fragments from a fragment created by another fragment, which, in turn, is created by MainActivity. (MainActivity → HomeFragment → AnotherFragment).
MainActivity.java
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener, TitleSetter, NavigationDrawerEnabler{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(myToolbar); getSupportActionBar().setHomeButtonEnabled(true);
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.home, container, false);
AnotherFragment.java
public class NewWord extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) {
Also drawer layout
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/container_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </LinearLayout> <FrameLayout android:id="@+id/container_body" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> </LinearLayout> <fragment android:id="@+id/fragment_navigation_drawer" android:name="com.calgen.wordbook.activity.FragmentDrawer" android:layout_width="@dimen/nav_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" app:layout="@layout/fragment_nav_drawer" tools:layout="@layout/fragment_nav_drawer" />
And menu_main.xml
<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"> <item android:id="@+id/action_search" android:title="@string/action_search" android:orderInCategory="100" android:icon="@drawable/ic_action_search" app:showAsAction="ifRoom" /> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" />
I actually mentioned this tutorial to set up a box fragment for navigation: Android Hive .
I also mentioned similar questions that were asked here on stackoverflow:
1. Switch between Android Navigation Drawer and Up caret images using fragments
2. My problem is similar to the following: Android navigator display indicator for fragments of a lower level . - I could not comment here, since I do not have 50 repetitions. I implemented what was said in the comments.
But still clicking on the top carriage does not start the onOptionItemSelected function, even if I have home input enabled.!
Now I am adding my custom styles.xml, maybe there is some error in this?
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyMaterialTheme" parent="MyMaterialTheme.Base"></style> <style name="MyMaterialTheme.Base" parent="Theme.AppCompat"> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:activatedBackgroundIndicator">@drawable/nav_background</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="android:textColorPrimary">@color/textColorPrimary</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
Also custom toolbar.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />