Implementing ActionBarSherlock Side Menu Navigation Drawer in Android

In this tutorial, you will learn how to implement an ActionBarSherlock Side Menu Navigation Drawer in your Android Application. The navigation drawer is a panel that transitions in from the left edge of the screen and displays the main navigation options. Using a Side Menu Navigation Drawer allows the user to easily navigate between fragments or activities. The navigation drawer can be brought onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar. We will create a custom side menu navigation drawer using ActionBarSherlock Library and on menu item click will show different fragment views. So lets begin…

Download the Latest Support Library

Download the latest support library revision 13 that supports the new Navigation Drawer. 

Link : http://developer.android.com/tools/extras/support-library.html

Replace the old support library (android-support-v4.jar) with the new support library in your ActionBarSherlock Library.

Support Package, revision 13 (May 2013)

Changes for v4 support library:
  • Added DrawerLayout for creating a Navigation Drawer that can be pulled in from the edge of a window.

Prepare your project by importing the ActionBarSherlock Library. Refer to Implementing ActionBarSherlock in Android tutorial.

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project SideMenuTutorial.

Application Name : SideMenuTutorial

Project Name : SideMenuTutorial

Package Name : com.androidbegin.sidemenututorial

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created some sample titles, subtitles and icons into a string and integer arrays and passed it into MenuListAdapter to display it on a listview as a side menu. On listview side menu click will display the selected fragment together with the title on the action bar. The ActionBarDrawerToggle facilitates the proper interaction behavior between the action bar icon and the navigation drawer.

We have prepared some sample icons for this tutorial. Insert your downloaded sample icons into your res > drawable-hdpi.

Sample Icons

SideMenu Sample Images (7.0 KiB, 3534 downloads)

Next, create an XML graphical layout for your navigation drawer. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file drawer_main.xml and paste the following code.

drawer_main.xml

Output:

Side Menu Navigation XML Layout  

Next, create a custom menu adapter class. Go to File > New > Class and name it MenuListAdapter.java. Select your package named com.androidbegin.sidemenututorial and click Finish.

Open your MenuListAdapter.java and paste the following code.

MenuListAdapter.java

In this custom menu adapter class, string and integer arrays are set into the TextViews and ImageViews followed by the positions.

Next, create an XML graphical layout for the listview menu item. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file drawer_list_item.xml and paste the following code.

drawer_list_item.xml

Output:

Drawer List Item

Next, create the first fragment. Go to File > New > Class and name it Fragments1.java. Select your package named com.androidbegin.sidemenututorial and click Finish.

Open your Fragments1.java and paste the following code.

Fragments1.java

Next, create the second fragment. Go to File > New > Class and name it Fragments2.java. Select your package named com.androidbegin.sidemenututorial and click Finish.

Open your Fragments2.java and paste the following code.

Fragments2.java

Next, create the third fragment. Go to File > New > Class and name it Fragments3.java. Select your package named com.androidbegin.sidemenututorial and click Finish.

Open your Fragments3.java and paste the following code.

Fragments3.java

Next, create an XML graphical layout for first fragment. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file fragment1.xml and paste the following code.

fragment1.xml

Next, create an XML graphical layout for second fragment. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file fragment2.xml and paste the following code.

fragment2.xml

Next, create an XML graphical layout for third fragment. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file fragment3.xml and paste the following codes.

fragment3.xml

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

In your AndroidManifest.xml, we need to change the theme style to “Theme.Sherlock” and set your preferable Android minimum SDK version. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

Side Menu Navigation ScreenShot

Source Code

ABSSideMenuTutorial (3.0 MiB, 5682 downloads)
  • http://gonzalobenoffi.com.ar/ Gonzalo Benoffi

    Excelent!!!!

    Working perfect!!

  • Ariel

    I got DrawerLayout error, and doesn’t seem to be an import fix. Any ideas? i got ABS 4.3

    • Ariel

      I fixed it using your ABS project. Maybe Drawer doesn’t work on latest version of abs

      • http://www.AndroidBegin.com/ AndroidBegin

        Hi Ariel, I forget to mention in my tutorial post. You need to download the latest Support Library from Google Support Library. Use the latest support library in your ABS Library and project.

        • Ariel

          I downloaded Support Library but still doesn´t work. However it works with your ABS. Is there a way to make the fragments landscape? I got a tablet and the fragments takes only half screen. Thnxs!

          • http://www.AndroidBegin.com/ AndroidBegin

            Hi Ariel, I don’t have a physical Android tablet so I tried it on a tablet AVD. Everything seems okay to me. Take a look at the screenshot. Its running on API 15.

          • Ariel

            Thanks for the answers. As you see on the images (the firstone is the emulator, the second is the eclipse layout), the content is moving to the left side of the screen. I can’t figure out why.

          • Ariel

            Sorry for that i got it now. Befor i update the Support Library i was getting an error when i defined “match_parent” for layout width and height on the drawer xml file. So i had to put the size un pixels, like 800×400, so i changed that now and works great. Thank you very much man and by the way great job.

          • http://www.AndroidBegin.com/ AndroidBegin

            Your welcome :)

  • Ricardo García Fernández

    Great easy and very useful , thanks !

  • James Giomatti

    Is it possible to implement the back button to return to previous fragment in the MainActivity?

    • http://www.AndroidBegin.com/ AndroidBegin

      I think it is possible. I will see what I can do. :)

      • http://www.AndroidBegin.com/ AndroidBegin

        After some thorough googling. You can try this to close your drawer on the back button click. I’m still working on returning to previous fragment. To give it a try, add this portion of codes in your MainActivity.java.

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Close Drawer on Back Key
        mDrawerLayout.closeDrawer(mDrawerList);
        }
        return super.onKeyDown(keyCode, event);
        }

        • J Giomatti

          This is great. Thanks for a detailed tutorial.

          • http://www.AndroidBegin.com/ AndroidBegin

            I think the best approach is to close or open the navigation drawer on back key. Just my suggestion. :)

  • Amyth

    Your tutorial is the best I have come across on this topic. Excellent and I really mean it. I was looking for something good on this topic since a few days.

    I was trying to implement something similar in my code using ActionbarSherlock. The code is working well. The only issue is that I am not able to toggle the “up” caret next to my app icon when the navigation drawer is pulled out.
    You can checkout my query on SO. http://stackoverflow.com/questions/16870646/not-able-to-change-the-up-caret-next-to-the-app-icon-in-the-actionbar-in-my-navi
    Kindly help if possible. Thanks.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Amyth, may I know what do you mean by “up” caret?

  • Ariel

    Hi, it’s me again >D. Is there a way to call a SherlockFragment from another? For example, inside fragment3 i want a button that calls a fragment 4, that is not on the side menu. Thnx!

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Ariel, its nice to see you back. I’m not sure if this is what you need. Check out the codes below.

      First of all add a button into your fragment.

      Then use the code below in your fragment with the button you created.


      public class Fragment2 extends SherlockFragment {
      Fragment fragment1 = new Fragment1();
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      final View rootView = inflater.inflate(R.layout.fragment2, container,
      false);
      final View button = rootView.findViewById(R.id.button);
      button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      FragmentTransaction ft = getSherlockActivity()
      .getSupportFragmentManager().beginTransaction();
      ft.replace(R.id.content_frame, fragment1);
      ft.commit();
      }
      });
      return rootView;
      }
      }

      • Ariel

        Thanks very much for the answer! I got it working great.

  • http://dooid.com/danielnadeau Daniel Nadeau

    Great tutorial, thanks!

    One issue, though, was that you accidentally used super.onDrawerClosed(View view) twice, making the animation in the action bar a little jumpy. Might wanna fix that ;)

    Also, your sample image for the shadow should be a 9patch. While the file appears 9patched, the filename doesn’t end with .9 :)

  • Phil

    A nice tutorial that closely follows the Android docs.

    Unfortunately there is a small bug with it which I’ve not been able to resolve. When the drawer is pulled out the icon correctly animates, but once it reaches the final open position the icon snaps back to where it started.

  • Phil

    I came back to your example when I was having issues trying to highlight the selected item in the list and noticed you’re also making a similar call:

    mDrawerList.setItemChecked(position, true);

    I wondered if it was something to do with the added complexity in my list (I have header, footer, separaters) but it appears it also doesn’t work in your example.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Phil, I’m not sure what your issue here. Could you please show us your codes? You can use http://pastebin.com/ if your codes are long and If you are concern about privacy you can email us your codes. :)

      • Phil

        If you see the attached images, the first is the Google demo app, you’ll see that the active fragment’s corresponding menu item remains highlighted, however on your demo it does not.

        I’ve even removed my custom adapter and used an ArrayAdapter with a CheckedTextView, but still cannot get highlighting to work with ABS.

        I’m just trying to get to the route cause of this issue and thought maybe you had noticed this with your demo. So far I can see that a number of ICS+ apps are working, Google nav drawer demo, Gmail, Google Play Music and that a number of ABS apps are not, your demo, my app.

        Not sure if it is relavent but It looks like Google Play Music is using ActionBarCompat now as it has ActionBar and Nav Drawer on Android 2.3 but unfortunately they still haven’t released this yet.

        I think the next step should probably be to try adding ABS to the Google demo and see what happens.

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi Phil, I get what you mean now. Just insert this into your drawer_list_item.xml LinearLayout

          Insert

          android:background="?android:attr/activatedBackgroundIndicator"

          into your LinearLayout.

          Sample Below

          • Phil

            I got it working in API 11+ using this method after hacking up the Google planets demo app a bit, but still no solution for Android 2.x devices. It kinda defeats the point of using ABS at all if you can’t fully support Android 2.x!

            The home icon doesn’t work in 2.x either, it just displays the up carat.

            I’m sticking Android 2.x on the todo list for now and I’ll come back to it later when the app is finished, thanks for your input.

  • UnjiRohOwlRock

    wonderful code!! i was just searching for the guideline for navigation drawer. and this one is the best among all the google search list. Thanks it helped alot.

    • UnjiRohOwlRock

      Actually i got one question, lets say i want to put multiple seekbars in drawer, and I want to give all of them a different jobs. Since i only have one id for seekbar in drawer_list_item.xml, where should i code for my seekbars? or where should my code for seekbar supposed to go? Thanks!

      • http://www.AndroidBegin.com/ AndroidBegin

        Hie Unji, you can just code it in your MainActivity.java class. :)

  • Anubhav

    Excellent tutorial.

  • Cali

    I am having a extremely hard time getting this to work. I imported the project and I keep getting this error http://pastebin.com/8M0H4p5h I dont even know where Lcom is coming from im not defining that in the path. Also when I go through the debug process I get this error NativeStart.main(String[]) line: not available [native method]. Any help is greatly appreciated as I am also new to android development like many others on here.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Cali, are you using the latest ActionBarSherlock Library and Support Library? or try using the ABS Library provided in the download. Follow the steps below.

      Step 1 : Download and Import both projects (ABS and ABSSideMenuTutorial) into your Eclipse.

      Step 2 : Right-click on SideMenuTutorial project and click properties.

      Step 3 : Import the ABS Library

      Step 4 : “Fix project properties” and “Clean” for both projects

      Step 5 : Restart Eclipse (This works for me)

      Let me know if you still have issues. Good Luck

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Cali, I have clean the projects for you, kindly please re-download the codes and follow the steps I mentioned on the previous post.

  • sinistance

    thanks! work awesome with holoeverywhere too.

  • Nicola Lombardi

    Hi,

    Wonderful tutorial, I will use this for my internship project.

    There is only one problem I can not resolve.
    The side menu is transparent, and I can see my fragments layout under it.
    In the demo it looks like the drawer comes above the fragment, I copied the code but still it looks like the drawer is beneath the fragment and not above it.
    I did some troubleshooting ;) before I came to this conclusion, if I set a background color on one fragment I can not see the side menu. Meaning the drawer is beneath it right? But I tried the whole weekend trying to fix it but I can not figure it out. Can someone help me.

    Thank you very much in advance :)

    Nicola Lombardi

    • Nicola Lombardi

      Hi there,

      I finally found my error, I was replacing the wrong content for the fragment, oops x)

      • http://www.AndroidBegin.com/ AndroidBegin

        Hi Nicola, I’m glad this tutorial helped you. :)

  • Mau

    Hi,
    I have the new ActionBarSherlock library in my eclipse, with the support library rev 13. I downloaded this package and imported SideMenuTutorial. Then I added the support library to it and also referenced the ActionBarSherlock Library.

    I have no errors, but when i run it, i get “Unfortunately SideMenuTutorial stopped working” sign.

    Any ideas?? what am i doing wrong??

    Thanks, excelent tutorial btw.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Mau, please do a “clean” on your project and the ActionBarSherlock Library. Then restart your Eclipse. To clean your projects, go to Project > Clean .

      Make sure your projects looks similar to the screenshot attached on this comment.

      If non of the above are working, try using ABS library I provided for you. Let me know if you need further assistance. :)

      • Richard

        I have the same problem I have no errors, but when i run it, i get “Unfortunately SideMenuTutorial stopped working” sign.

        I tried to Clean en almost everything looks te same only one thing different.

        I use your ABS library..

        Only different is android-support-v4.jar is’nt in the Referenced Libraries but it is Android private Libraries

        To fix this problem: from(https://groups.google.com/forum/#!msg/adt-dev/epOfZbKPFdk/RbR2VYNQ5_8J)

        When upgrading, the ‘Order and Export’ of the new ‘Android Private Libraries’ is not always checked. And the android-support-v4.jar is now in this ‘Android Private Libraries’ section.

        To fix this, go to ‘Order and Export’ and check ‘Android Private Libraries’. Then refresh/clean/rebuild.

        After you done this ‘fix’ for a library project, you may need to just close and re-open any depending project, because they may not see this ‘fix’ immediately.

        See image!

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi Richard, I feel your frustrations. Lets try it this way, temporary remove all the apps in your Eclipse. Then download a copy of this tutorial, then try to compile it again. Do the necessary “cleaning” and “Fix Project Properties”, then restart Eclipse. Let me know if you still having issues.

  • kawingtamtkw

    how can I change the words of the side bar according to the @string/XXX?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi kawingtamtkw, the side bar itself is a ListView. Doing that will require rewriting the whole source code. :)

  • kawingtamtkw

    I want to add an onClickListener in the Fragment1.java, but there’s are some errors, How can I fix it?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi kawingtamtkw, we have previously written a tutorial about button click new activity example. You can check it out here http://www.androidbegin.com/tutorial/android-button-click-new-activity-example/ . However, I will show you how to implement it. See below

      public class Fragment1 extends SherlockFragment {
      private Button button;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment1, container, false);
      button = (Button) rootView.findViewById(R.id.button1);
      button.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
      // TODO Auto-generated method stub
      Toast.makeText(getSherlockActivity(), "Button Clicked",
      Toast.LENGTH_LONG).show();
      }
      });
      return rootView;
      }
      }

      • kawingtamtkw

        I’ve read your passage of “Android Button Click New Activity Example”, but it is still unsuccessful. Can you tell me how to fix it?
        Thanks a lot:))

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi kawingtamtkw, I’m not sure if this is what you need. Check out the codes below.

          public class Fragment1 extends SherlockFragment {
          private Button button;
          Fragment fragment2 = new Fragment2();
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment1, container, false);
          button = (Button) rootView.findViewById(R.id.button1);
          button.setOnClickListener(new OnClickListener() {
          public void onClick(View arg0) {
          // TODO Auto-generated method stub
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.replace(R.id.content_frame, fragment2);
          ft.commit();
          }
          });
          return rootView;
          }
          }

          • kawingtamtkw

            How can I solve these problems?

          • http://www.AndroidBegin.com/ AndroidBegin

            Check your imports. :)

          • kawingtamtkw

            imports means?
            what imports should I add?

          • http://www.AndroidBegin.com/ AndroidBegin

            Try put this imports into your fragment1.java


            import android.os.Bundle;
            import android.support.v4.app.Fragment;
            import android.support.v4.app.FragmentTransaction;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.view.ViewGroup;
            import android.widget.Button;

  • i.dragon

    Hi, I am a beginner in android.
    I notice that you use a textView and an imageView for your ListView item,
    I wonder if I could just simply use a textView but with drawable icons along the left hand side to achieve the same effect ,for example,in the getView(…) method :

    mtextView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.action_settings), null, null, null);

    any help will be appreciated.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi i.dragon, the left side menu drawer is written using a ListView, changing it will require rewriting the source code. You will have make your own changes. However, if you are having issues feel free to comment here. :)

      • i.dragon

        I got it and thanks! I had totally ignored that there is also another textView for a subtitle in your code. therefore an imageView fits well. By the way,the tutorial is wonderful.

        • http://www.AndroidBegin.com/ AndroidBegin

          Glad it helped. :)

          • http://www.Teach-Watch.com Rony Tesler

            i.dragon, can you please show how you did it?

  • Loui

    Hi

    Will this work if I test it on device with android version 2.2.2?

    I haven’t been able to launch avds for a while now and I’ve tried everything from google. Maybe I missed something. Therefore I’ve been using my device to test projects.

    Thanks

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Loui, the lowest API tested is API10(Android 2.3.3). I will test others when I have time. :)

  • Pavlos

    Well i cant seem to get the proper Navigation Drawer icon on devices below ICS. I just get the usual ‘Up’ icon displayed on 2.3 and below

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Pavlos, I just realize that issue. Thanks for reporting. :)

      • Pavlos

        Is there any workaround? I have googled about it a lot and cant seem to find anything related.

        • http://www.AndroidBegin.com/ AndroidBegin

          Same here, I will post my updates here. Meanwhile, I will try to find help from other sources. :)

          • Cameron Voell

            Did either of you guys ever find another solution to the sliding navigation drawer icon not showing up on pre ICS with ActionBarSherlock? If I have to drop ABS just for the three little lines, I might cry. Thanks!

          • http://www.AndroidBegin.com/ AndroidBegin

            Hi Cameron, create a thread in our forum and discuss this matter over there. :)

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi Pavlos, I found a temporary fix. Try the codes below.

          styles.xml

          @drawable/ic_drawer

          AndroidManifest.xml

          • Pavlos

            Nice one mate. Thank you i will apply it to my application and tell you.

    • http://www.Teach-Watch.com Rony Tesler

      anything new about it?

  • Rahul

    Hello sir just wanted to know can we implemented this drawer on bottom of the screen.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Rahul, I don’t think its possible using navigation drawer. But there is an option, you can try create an actionbarsherlock options menu at the bottom, then set the menu to open fragments. :)

  • m1nd

    If I copy your project 1:1 and import it into eclipse I get errors that these 3 imports can’t be resolved:

    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v4.view.GravityCompat;

    Do you know how to fix that? Everything should be updated and working fine. I’m really frustrated right now. I’m trying to fix that for hours now but nothing works..

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi m1nd, check out the screenshot attached on this comment. You need to import the library from the ActionBarSherlock Library or copy the android-support-v4.jar from ABS library and paste it into SideMenuTutorial lib folder.

      • m1nd

        That did the job, everything works now.
        Thanks mate ;)

  • Arafayé

    Hi. Is there a way to use the Holo light theme on the drawer ? If i switch to Theme.Sherlock.Light.DarkActionBar i see the drawer black :/

    anyway awesome job ;)

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Arafaye, have you tried this? android:theme=”@style/Theme.Sherlock.Light”

  • Bradford

    I’m trying to get this going, however when I run I get the following error:

    NoClassDefFoundError android.support.v4.widget.DrawerLayout

    I have no errors before it compiles, however as soon as my code references a DrawerLayout, it crashes. I wonder if it’s still using the old r7 code. I’m also using Maven.

    Any suggestions?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Bradford, you have to use the latest Support Library. Try to use the ABS library provided in the download. :)

    • Bradford

      Nevermind, I found my problem. Maven was still using the old support-v4. I had to install the new one to my local repo using this command:

      mvn install:install-file -Dfile=android-support-v4.jar -DgroupId=com.google.android -DartifactId=support-v4 -Dversion=r13 -Dpackaging=jar

  • Matimu

    I want to add more items in the ListView but categories them e.g
    category 1 contains Fragment 1,2 and 3.
    category 2 contains Fragment 4,5 and 6.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Matimu, check on this docs from Android. http://developer.android.com/reference/android/app/ExpandableListActivity.html Expandable ListView is what you need. :)

      • Matimu

        Thanks i will get back at you soon

      • Matimu

        @AndroidBegin:disqus Unable to To separate The Title and Subtitle in the side menu in categories am getting the category name on top of every title how do i split it .

      • Matimu

        AndroidBegin On MenuListAdapter am able to pass mMenuAdapter = new MenuListAdapter(this, category,title,subtitle, icon);

        am unable to get the other two categories on the side menu

  • Anonymous

    Hi, I want to implement tabs into one of the fragments (like the new Google Play Music App does), but I can’t find the way to do it.

    Thanks ;)

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi there, have you seen our previous tutorial regarding fragment tabs? http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-fragment-tabs-in-android/

      • Anonymous

        Of course I’ve seen that tutorial, and I’ve tried also with ViewPager Tabs, but the problem is when I try to implement the “SherlockFragmentActivity” in one of the “FragmentsX.java” (this tutorial), because each “FragmentsX.java” must extends from “SherlockFragment” instead of
        “SherlockFragmentActivity”.

        Thanks

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi there, try using getSherlockActivity().getSupportActionBar();

          In your FragmentsX.java


          public class Fragment1 extends SherlockFragment {
          // Declare Tab Variable
          Tab tab;
          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Create the Actionbar
          ActionBar actionBar = getSherlockActivity().getSupportActionBar();

          Let me know if this works :)

  • Liran

    Please answer this question:

    It seems that in order to open the drawer by using a gesture would work only if the touch starts from the most left side of the screen.
    How can I customize the area that would trigger this gesture , to move the navigation drawer?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi there, I’ll see what I can do. At the meantime, try googling or post a question regarding this on stackoverflow. :)

  • username

    Thanks for this great tutorial.I have one question though.Instead of a TextView I want to display an ImageView but I cant get it to work, the ImageView isn’t displayed at all.The image I want to use is located on the sd card in my device.I’ve been working on this for days now :( I hope you can help me.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi there, if you are looking at the source code. The listview that is used for the menu contains images that is located in your res folder. You can change any images you want from there. :)

      • username

        :) thats not what I meant.I want to add an ImageView in the “fragment1.xml” for example.Any idea why it is not working for me?Thanks for your quick answer :)

        • http://www.AndroidBegin.com/ AndroidBegin

          Hi there, try the codes below. Let me know if its working for you. :)


          public class FragmentTab1 extends SherlockFragment {
          ImageView image;
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragmenttab1, container, false);
          image = (ImageView) getSherlockActivity().findViewById(R.id.image);
          image.setImageResource(R.drawable.image);
          return rootView;
          }
          }

          • username

            Gives me a null pointer exception :S caused by the line image.setImageResource(R.drawable.image).

          • http://www.AndroidBegin.com/ AndroidBegin

            Hi username, did you manage to show the image when you use the rootView? :)

          • username

            I tried that too, once in the onCreateView and once in the onActivityCreated. Both methods ended up not showing the ImageView,however a TextView or a simple Button is displayed.This is driving me nuts lol :D

          • http://www.AndroidBegin.com/ AndroidBegin

            Ok, I’ll test my codes again. :P

          • username

            To sum up getSherlockActivity -> null pointer exception, rootView -> no image.Thanks so much for your time so far.This is a really important project to me.

          • username

            Finally!I figured it out myself and guess what,of course it was my fault.I wrote a piece of code to get the filename with the path from the image located on the sd card.There was the problem for some reason.Thanks so much for your time :)

          • http://www.AndroidBegin.com/ AndroidBegin

            Changing it to rootView seems working for me. I’ve edited my comment above. Thanks

  • http://franciscovelazquez.com/ Francisco Velazquez

    Is there a way to support

    ?android:attr/activatedBackgroundIndicator

    in your drawer_list_item.xml layout?

    in 2.3 using Actionbar Sherlock? Do you have a tutorial that shows how to display the selected navigation drawer item? Thank you, and great tutorial.

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Francisco, insert this into your drawer_list_item.xml LinearLayout to get it Highlighted.

      Insert

      android:background="?android:attr/activatedBackgroundIndicator"

      into your LinearLayout.

      Sample Below

  • Salim Yego

    Am trying to load a listviev on the fragment when an item on the side menu is clicked but am having a problem..the listview is populated from a json array…i already have the class generates the listview and is working okay..how do i intergrate it?

    • Tharaka Nirmana

      I am also having the same issue, I found out that the original layout items below the slider prevent items from touching. I cant find a solution. Any tip?

  • VenomVendor

    Can you give an example of having tabs in Menu?
    Reference image, http://i.stack.imgur.com/HuWYO.jpg

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi, I’ve already created a tutorial regarding your request, check it out here http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-side-menu-navigation-with-fragment-tabs-tutorial/

      • VenomVendor

        Nope, The other way.
        Tabs should be inside menu. Plz recheck the reference image.
        No one has achieved/tried it as of now. I bet you.

        • http://www.AndroidBegin.com/ AndroidBegin

          I just tried it but end up getting this error “android:id/tabhost} is not a drawer” . I’ll try another way around. :)

        • http://www.AndroidBegin.com/ AndroidBegin

          Yes, it is possible. Check Screenshot

          Here is the hint. :)

  • GianMarcoDiFrancesco

    hi i have fragment with gps status and info but the fragment re-create every time when pass from another fregment to this….how keep state of my fragment whitout re create it?

  • Wasseem Bazbaz

    this tutorial was a great help.
    But am facing an issue the fragment i made gets recreated for example if am doing something with current fragment like running something a new fragment is made some help pleasE ?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Wasseem, I’ll try to figure it out. :)

  • Francisco Rojas Morales

    Hi! how i could return a map fragment in this tutorial?

  • Kynkeniven Grosskopf

    Hi, I’m having a problem in drawer_lis_item.xml

    on android:background=”?android:attr/activatedBackgroundIndicator”

    it says “requires API level 11 (current min is 8)”

    if I remove that line, my tabs get untidy

    O my device runs ok with that line,

    but in device with 2.3 or less get “android.view.InflateException: Binary XML file line #2: Error inflating class ”

    can you help me?

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi Kynkeniven, thanks for your feedback. I will look into it.

      • leo_lv

        Hi, I also have the same problem, and I was by changing the background listview item is selected to achieve the effect,I hope you have a better way

  • DroidNoob

    How would you have the button displayed on the right side and open from right to left?
    Thanks

  • sakthidasan

    hi
    if i click one button.. the menus are open as slide from left to right for example(picart app)
    .. and then i want create my menus are vertical if i click any one menu will open the submenu from right to left as slide movement
    if u have answer pls help me

  • Ivan

    How can I add “headers” to the drawer menu? I need to add 3 headers for my drawer’s list.

  • Petar Papalevski

    I found a solution for the force close in android 2.3. Just delete this two lines in drawer_list_item.xml

    style=”?attr/spinnerDropDownItemStyle”
    android:background=”?android:attr/activatedBackgroundIndicator”

    • mahmut efe

      Thank you so much Petar Papalevski. I deleted related lines and it worked on android 2.3 version.

    • Çağrı Çakır

      Great catch! I’ve checked the attr/ path in integrated sherlock project inside my project and those attributes were already defined in it. What is the reason that such a mix comes up? Regards.

  • magnificat

    Hello and thank you for this great tutorial!

    How can I add a Webview in fragments ?

  • mahmut efe

    this sample code is not working on old version. i tried on 2.2 and 2.3. it didnt worked. but it is working on 4.0 and upper.

  • Alan Dwi Prasetyo

    eror :(

  • Alan Dwi Prasetyo

    eror, is there anything wrong?

  • Frank

    Hello,

    absolute great tutorial. Thanks for sharing :-)
    One question: Is a category also possible?

    Thanks…

    • mitsus

      Hi, have you solved about category? If yes can you shere your solution? Regards

      • http://www.AndroidBegin.com/ AndroidBegin

        Hi, I’ll prepare a tutorial on that. Stay tuned. :)

  • Alexmuller1416

    how to use intent in the project

  • Esteban Butti

    Hey! I’m trying to put some of the side menu options on the bottom of the menu, so I should have 2 lists, one on the top and other on the bottom. Can you guide me on how to achieve that? Thanks in advance.

  • HG

    this dont work

  • Ejumu Emma

    very good but when are coming up with that of categories thankyou

  • Tharaka Nirmana

    Working fine, thank you!. But there is a problem. When I click on an item, the layout below the slider prevents slider items being clicked, when I remove all below layout elements, slider items are clickable. Please help!

  • Mahdi Kenji

    how can make slide menu right of screen?

  • Capkin

    hi, thanks for the tutorial it’s very good. But I have a question how can I add to this app Imageloader form Internet. thanks a lot

  • Aazam Khan

    Hiii nice tutorial but i want both side (left drawer and right drawer ) plz help me out ..

  • http://www.Teach-Watch.com Rony Tesler

    I’m using sherlock actionBar and I set a customView to the actionBar. So I have one strip that contain the drawer icon and my custom view, and another strip with the tabs of the action bar.
    How do I change the background color of the drawer icon? it’s not the same as the tabs’ color.

  • Mahdi Kenji

    thanks a lot. it was cool tutorial. but can we make this menu right to left?

  • http://vk.com/zh.madiyar Madiyar

    How do when swipe,nav drawer swiped with action bar
    Like this

  • nilesh kansal

    How do when swipe,nav drawer swiped with action bar
    Like this

  • Faisal

    i am new in android. if we want this side menu in every activity, then do we have to do this in each activity?