Created
November 18, 2015 16:43
-
-
Save eirabben/46d81bd9db52325563c7 to your computer and use it in GitHub Desktop.
Android navigation drawer inside fragment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.DrawerLayout | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<include layout="@layout/toolbar" /> | |
<!-- Actual fragment content --> | |
</LinearLayout> | |
<android.support.design.widget.NavigationView | |
android:id="@+id/navigation_drawer" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:background="@android:color/white" | |
app:menu="@menu/drawer_view" /> | |
</android.support.v4.widget.DrawerLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainFragment extends Fragment { | |
private DrawerLayout mDrawer; | |
private Toolbar mToolbar; | |
private ActionBarDrawerToggle mDrawerToggle; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setHasOptionsMenu(true); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_main, container, false); | |
mToolbar = (Toolbar) view.findViewById(R.id.toolbar); | |
AppCompatActivity activity = (AppCompatActivity) getActivity(); | |
activity.setSupportActionBar(mToolbar); | |
mDrawer = (DrawerLayout) view.findViewById(R.id.drawer_layout); | |
mDrawerToggle = new ActionBarDrawerToggle( | |
getActivity(), mDrawer, R.string.drawer_open, R.string.drawer_close); | |
// Where do I put this? | |
mDrawerToggle.syncState(); | |
return view; | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
mDrawerToggle.onConfigurationChanged(newConfig); | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
super.onCreateOptionsMenu(menu, inflater); | |
inflater.inflate(R.menu.menu_main, menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (mDrawerToggle.onOptionsItemSelected(item)) { | |
return true; | |
} | |
switch (item.getItemId()) { | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/tools" | |
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" /> | |
</android.support.design.widget.AppBarLayout> |
Waste
code is waste don't use it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so weird . I'm new in android. I use Kotlin for android. I search about 3 hours .I had got mad for importing navigation drawer to just one of my fragment !! everything I did it caused that error : "java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY. " for this I used specific height for its parent layout. like 35dp. after that it showed NavigationView menu drawer but it was in small height . I mean NavigationView height was same as DrawerLayout 35dp . Completely incorrect. I was to lucky to finding your GitHub codes , that was for 5 years ago ! wow. I find out my whole layout should be between DrawerLayout Tag. instead of " " in your code. I have coordinator layout as root layout and many recyclerview and nested scrollview. I was to confused. I tried to import all navigation and drawable layout between them. android programing is too hard for after many years programming in other platform . I have not found that rules yet too complex rules or implementing component in UI and logic.. Thank you for sharing your knowledge.