Search code examples
androidmenufragmentandroid-tabhostsliding

Sliding menu Android


I have an app that is using tabHost menu and I need to change it, because the menu items is growing and they don't fit in the current menu. My classes are extending FragmentActivity, I want to know if is possible to use Sliding Menu with them or I will need to change all classes to extend Fragment.

Thanks in advance.


Solution

  • If you use jfeinstein10/SlidingMenu in your project, you can implement sliding menu in your FragmentActivity, as you want.

    step1. new SlidingMenu and attach to your activity.

    Refer to How to Integrate this Library into Your Projects:

    You can wrap your Activities in a SlidingMenu by constructing it programmatically (

    menu = new SlidingMenu(Context context)) and then calling

    menu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT).

    SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.

    step2. Then implement a SampleListFragment which represent your menu, and add to your activity.

    define a fragment container layout fragment_menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    call menu.setMenu(R.layout.fragment_menu); to add the fragment container view to your activity,

    Lastly, add your SampleListFragment to your activity's fragment manager:

        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.fragmentContainer, new SampleListFragment())
        .commit();
    

    In this way, you got a sliding menu in your fragment activity. That's all.