Android Hamburger Menu Interaction - android

I am working on an android app where there is a drawer layout in the actionbar. I have added listeners for the drawerlayout to listen for when the drawer is opened or closed. There is a navigation view attached to the drawerlayout.
Now the drawer can be opened and closed in various ways : tap-to-open, swipe-to-open, swipe-to-close and tap-outside-to-close. I have to add events to identify each of these separately.
I have added the drawerlayout and navigationview in this way :
<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"
/>
Noe in the Activity, I have added the following listeners on navigationview and drawerlayout.
//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()){
// Switch case identify different options
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,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
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
Now onDrawerOpened and onDrawerClosed gives me information only about when drawer is opened and closed. How do I distinguish between interactions to open and close ? Interactions to identify : tap-to-open, swipe-to-open, swipe-to-close and tap-outside-to-close.

The ActionBarDrawerToggle offers more than just the onDrawerClosed, onDrawerOpened methods. You override the onDrawerSlide method to detect slides:
..
#Override
public void onDrawerSlide (View drawerView,float slideOffset){
if(determine if openning or closing){
doStuff();
}else{
doOtherStuff();
}
}
Taps on the toolbar can be deteced by overriding the Activity.onOptionsItemSelected(MenuItem item) method. Then check if the item has the id android.R.id.home.
Read more here: https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

I have found a way to accurate determine if drawer was opened by sliding, using some existing parameters and writing a different method:
#Override
public void onDrawerStateChanged(int newState) {
if (newState == DrawerLayout.STATE_DRAGGING) {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
// drawer is currently open and is being closed by sliding
} else {
// drawer is currently closed and is being opened by sliding
}
}
}
I will add for detecting the tap events correctly, once I get to know. If somebody know, please comment.

Related

How to disable manual sliding-out of navigation view

I have two navigation views in my Activity. One enters from the right and the other enters from the left.
In the navigtionview that enters from from the left, different fragments are started when when the items are clicked. And also, this same navigationview has menu items which is common to all the launched fragments. I don't have any problem with this one.
Now, the navigationview that enters from the right has menu items which are only peculiar to the particular fragment started when the first item in the left entering navigationview is clicked. What this means is that, when you clicked the first item in the left entering navigation drawer, a fragament is started, and items in the right entering navigationview has items related to this fragment.
So, this right navigationview is stared when a menuitem in the toolbar is clicked. And this menu item is not visible when other fragments (apart from the aforementioned) is in view.
The problem I have is that, even when the right entering navigationview cannot be launched through the menuitem in other fragments, it can still be started by sliding the right edge of the screen. So I want to totally disable the sliding feature of this right entering navigationview, so it can only be launched when the menu item is clicked.
Codes
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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="false"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
<android.support.design.widget.NavigationView
android:id="#+id/cat_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="false">
/** This navigationview enters from the right, I start a fragment with framelayout below.
The fragment contains a recyclerview **/
<FrameLayout
android:id="#+id/transport_cat_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Snippets of MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
}
#Override
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
}
};
drawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
}
}
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View v){
super.onDrawerClosed(v);
}
#Override
public void onDrawerOpened(View v) {
super.onDrawerOpened(v);
}
};
drawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment;
if (id == R.id.menu_cars) {
fragment = new CarsFragment();
startCarsFrag() //Method to start CarsFragment()
//The right entering drawer should only be enabled for this fragment
}
if (id == R.id.menu_trains) {
fragment = new TrainsFragment();
startTrainFrag() //Method to start TrainsFragment
}
if (id == R.id.menu_lorries) {
fragment = new LorriesFragment();
startLorriesFrag() //Method to start LorriesFragment
}
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
The DrawerLayout#setDrawerLockMode() method is what you're looking for. When locked, a drawer View cannot be dragged open/closed, though it will still respond to the openDrawer() and closeDrawer*() methods.
Since you're using two drawers, and want to lock only the one, you'll need to call the method with a second argument to indicate which drawer to lock/unlock. For example, to lock your secondary drawer closed:
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);

How to create hamburger menu in android simple way with reusable code?

I know how to create android hamburger menu, many tutorials are available.
I want to create Hamburger menu in each class, to call one method in common class which method have hamburger menu code. Also my hamburger menu inside listview with options.
How to create like this hamburger menu. I want to reduce my code and provide reusable code to others. Please help me.
You need to take an Activity and in that Activity's layout file put the following code:
<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_layout"
android:layout_width="match_parent"
android:elevation="4dp"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_new">
<put your layout here................>
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#drawable/bg_all"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:theme="#style/list_item_appearance"
app:menu="#menu/drawer_menu" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
After that in your Activity file:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView=(NavigationView)findViewById(R.id.navigation);
if (drawerLayout != null) {
drawerLayout.setDrawerShadow(R.drawable.list_back, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this, drawerLayout,
toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
And then take some fragments for adding the same navigation menu in every layout and call them like:
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 Which is our Inbox View;
case R.id.drawer_home:
txt_title.setText("Home");
Intent intent=new Intent(HomeActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
return true;
// For rest of the options we just show a toast on click
case R.id.drawer_artist:
txt_title.setText("Artists");
android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new ArtistsFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
}
});

Fit navigation drawer icon to toolbar in android

Hello,
I am working in android, in my app i am using navigation drawer and i have changed the using navigation icon as in the image. But that image is not getting fit to toolbar. as you see in image automatically margin is felt around the icon. i don't want that margin, that icon should fit to toolbar
below is my code
Please help
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawer.openDrawer(Gravity.LEFT);
}
});
// Drawer object Assigned to the view
mDrawerToggle = new ActionBarDrawerToggle(this, Drawer,toolbar, R.string.a, R.string.b) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
}; // Drawer Toggle Object Made
Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menuicon); //this is navigation icon
The default action bar will not allow you to cover the complete area. Instead, you can use custom Toolbar. In that case, you will have to handle clicks in the home menu etc by yourself.
Refer the following design for toolbar:
layout/custom_app_bar.xml
<android.support.v7.widget.Toolbar
...
... >
<LinearLayout
...
android:orientation="horizontal">
<ImageButton
...
android:background="#android:color/transparent"
android:src="#drawable/your_icon" />
<TextView
...
android:text="title" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Include this toolbar in your activity layout as
<include layout="#layout/custom_app_bar"/>

Navigation Drawer is not opening when sliding from left

As the page on Android Developers says
The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.
But strangely the navigation drawer on my activity does not respond to sliding action. It toggles only on touching the icon on the action bar. Below is my implementation of the navigation drawer
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
drawerLayout);
Is there any possible explanation for this? What I doubt is my activity by default has the layout of one of its fragments. So is that the reason?
Edit: The layout file of my activity
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:name="com.example.android.handsfree.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
Your ´NavigationDrawerFragment´ already creates a drawer Toggle for you inside its `setUp´ method.
You should not create a new one inside your ´MainPage´ Activity.
Notes:
You can use the android.support.v7.app.ActionBarDrawerToggle instead of the v4 one inside the NavigationDrawerFragment.
Update:
The problem seems solved now. There were 2 Issues:
The OP created a second drawer toggle in it's MainPage - Activity but there was already one created inside the NavigationDrawerFragment's setUp method, which gets called by the MainPage in order to set up the drawer. (This is basicly outsourc[ecoding]ing some of the drawer stuff to the drawer fragment.)
The OP locked the drawer inside onCreateOptionsMenu by calling a method which sets the DrawerLockMode to LOCK_MODE_LOCKED_CLOSED. He never reverted this change.
You should use:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/drawer"
android:choiceMode="singleChoice" />
on your activity
drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listData = (ListView) findViewById(R.id.listview_drawer);
drawlayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
Add these fields in your activity class:
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
Then, inside your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
ic_drawer is a .png image which located in drawable resources. You can search this image on Google.
API level to be used is 11+.
Hope it helps for you.
1- replace the tag by :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<ListView android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
2- use this code in the onCreate function u can use the v4 ActionBarDrawerToggle or the v7 her i use the v4:
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
R.string.app_name,R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

NavigationDrawer ListView does not show up

I use the ActionBarDrawerToggle to open and close the DrawerLayout.
But the listView inside my drawerlayout is not shown.
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setBackgroundColor(getResources().getColor(R.color.abs__background_holo_light));
mDrawerList.setAdapter(menuAdapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer,
R.string.open, R.string.close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
It shows only a black drawer without entries.
The drawerList should be white as set in "setBackgroundColor" and the drawerList should display the entries form the adapter.
When I open the drawer with openDrawer(mDrawerList) it works, but not on swipe.
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
//if (mDrawerToggle.onOptionsItemSelected(item)) {
// return true;
//}
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
Here is the main.xml layout with the DrawerLayout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content -->
<fragment
android:id="#+id/activeRemindersList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.colapps.reminder.fragments.ActiveRemindersFragment" />
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
Can anybody help?
UPDATE:
The problem seems is the Fragment.
If I add instead of the Fragment a simple FrameLayout all is working fine:
<!-- The main content -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test" />
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
Problem solved!
The problem was that in the Fragment Layout was also a "DrawerLayout".
I have changed a lot and forgotten to remove it from the Fragment Layout.
Thanks to all who have tried to help me.
I think you are missing a <FrameLayout> in your XML (notice the one I have wrapping the fragment content).
It should look something like this:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/app_info_fragment_phone"
... />
</FrameLayout>
<ListView
android:id="#+id/drawer"
... />
</android.support.v4.widget.DrawerLayout>
I am quite sure this has either something to do with:
the ListView not having a weight property
the ListView adapter being empty
the ListView being hidden by the Fragment
Try changing the layot file so that the ListView has a weight property as well and change the height property of the Fragment. Furthermore, put both of them in a LinearLayout.
Also make sure you have thesemethods overridden in your Activity:
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else
return false;
}
UPDATE:
Use the onDrawerItemSelected(int pos) method to add / replace Fragments.
#Override
public void onDrawerItemSelected(int pos) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new FragOne();
break;
case 1:
fragment = new FragTwo();
break;
case 2:
fragment = new FragThree();
break;
}
// R.id.content_frame is the id of the FrameLayout
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit()
// update selected item then close the drawer
mDrawerList.setItemChecked(pos, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
Here you have a detailed tutorial / example of how to implement the NavigationDrawer. When I first used the NavigationDrawer I went by this tutorial and was able to successfully implement it.
http://developer.android.com/training/implementing-navigation/nav-drawer.html

Categories

Resources