Monday 21 April 2014

Navigation Menu/Android Sliding Menu Sample

Hello All,

By this post I am gonna explain about how to integrate the Navigation Drawer like Facebook, Gmail, Google+ apps.

First you have to know whats mean by Navigation Drawer:

1. Navigation Drawer: 


The navigation drawer is a panel that displays the app’s main navigation options on the left edge of the screen. It is hidden most of the time, but is revealed when the user swipes a finger from the left edge of the screen or, while at the top level of the app, the user touches the app icon in the action bar.

you can get more info about this here. Good news is its now available in the SupportLibrary itself. 


How to Create the Drawer Layout ? 


To add a navigation drawer, declare your user interface with aDrawerLayout object as the root view of your layout. Inside theDrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.

For example, the following layout uses a DrawerLayout with two child views: a FrameLayout to contain the main content (populated by a Fragment at runtime), and a ListView for the navigation drawer.


<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 view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Here is the code to do in MainActivity.java

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    ...

    public void onCreate(Bundle savedInstanceState) {
        ...

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    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;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    ...
}


You can load multiple no of Fragments into the NavigationDrawer like as follow 


/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

// update selected item and title, then close the drawer
// mDrawerList.setItemChecked(position, true);
//mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}


}

2 comments:

Unknown said...

Easy Battery Saver, your super battery savior
Does your phone always need charging, even once a day?
Does your phone always die at crucial moment due to running out of battery?
Want to find a perfect solution?
Now is your answer!
Save power, extend battery life and perfect experience of using phone with Easy Battery Saver!
Easy Battery Saver is a powerful yet easy to use power manager app specifically designed to extend battery life and optimize using habit.
Easy Battery Saver saves your battery by intelligently dealing with phone’s network connectivity, screen time out and screen brightness. What’s more, the considerable sleep schedule setting will save your battery when you are sleeping.
Easy Battery Saver is easy to use by four preset saving modes and advanced customized mode. Just choose one mode, the battery problems will go away.
General Saving Mode, it has the basic network control, screen control and the sleep schedule to meet your normal battery saving needs.
Intelligent Saving Mode, it has the eight different controls and runs intelligently to save more battery than general saving mode.
Super Power Saving Mode, it can keep your phone's standby time as long as possible; you won't have to worry about whether your phone dies at crucial moment due to running out of battery any more.
Advanced Customized Mode, it is the best choice for fanciers; you can set all the values as you like, let your phone running in your way. The more, waiting for you to find!
Normal Mode, it will do nothing for your battery.
Also, tutorials in Easy Battery Saver will tell you how to deal with multi tasking and how to charge your battery healthily.
No matter what the situation, no matter who you are, Easy Battery Saver can always find a suitable solution for you.
Just have a try, you’ll find it really a good assistant! It is free!
You can get this app from http://droidmarkets.com/category.php?category=TOOLS

Blog27999 said...

As claimed by Stanford Medical, It's indeed the one and ONLY reason this country's women live 10 years longer and weigh on average 42 pounds lighter than us.

(By the way, it is not about genetics or some hard exercise and really, EVERYTHING around "HOW" they are eating.)

P.S, What I said is "HOW", not "what"...

CLICK this link to see if this little test can help you discover your real weight loss possibilities