1

I want to build a fixed mini nav drawer in android. will will always be visible an not extendable, with icons only.

Something like https://github.com/mikepenz/MaterialDrawer Mini Drawer. I try used his library, but I can't figured out how to make it fixed.

here is my code:

private Drawer result = null;
private MiniDrawer miniResult = null;


result = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .withTranslucentStatusBar(false)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("1").withIcon(FontAwesome.Icon.faw_home).withIdentifier(1),
                        new PrimaryDrawerItem().withName("2").withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2).withSelectable(false),
                           new DividerDrawerItem(),
                        new ToggleDrawerItem().withName("3").withIcon(FontAwesome.Icon.faw_home).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)
                ) // add the items we want to use with our Drawer
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        if (drawerItem instanceof Nameable) {
                            Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                })
                .withGenerateMiniDrawer(true)
                .withSavedInstance(savedInstanceState)
                // build only the view of the Drawer (don't inflate it automatically in our layout which is done with .build())
                .buildView();


        miniResult = result.getMiniDrawer();
        View view = miniResult.build(this);
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Mr T
  • 1,409
  • 1
  • 17
  • 24

3 Answers3

2

As of the normal usage there is always the "normal" drawer (via the DrawerLayout), and the MiniDrawer in your case you just want to use the MiniDrawer and add it to your View hierarchy.

As you already figured out correctly the MiniDrawer is filled via the normal DrawerBuilder as this comes with all the methods to interact with the elements added to the drawer.

As your use-case is special there is no "out-of-the-box" inflating of the MiniDrawer alone.

So you have the View of the MiniDrawer above. You now just need to add it to your Activity .

I recommend that your layout looks something like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- Place your content here -->
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

So in your code you get the "container"

LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(view, 0); //view is the view of your MiniDrawer

Just to improve your code and remove unnecessary stuff. You can remove

.withToolbar(toolbar)
.withTranslucentStatusBar(false) 

as those are not necessary if you just use the MiniDrawer

mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • The width of the mini drawer is too big, is there away to manually customize it? Thanks for your comment I really appreciate it. – Mr T Jul 13 '16 at 07:04
  • So you need less than `72dp`? All of the `MiniDrawer` is optimized and defined for those `72dp` it is possible to change it, but will require a bit more work – mikepenz Jul 13 '16 at 07:48
  • So it sounds it is working now. So you might accept the answer, and open a new question on how to change the width of the `MiniDrawer` will add a detailed answer for the new question too, later today. Will help others too – mikepenz Jul 13 '16 at 07:51
0

In the docs for that library the developer mentioned that you can lock the drawer. Could you try that out and see if it prevents it from opening?

result = new DrawerBuilder()...
...
//get the DrawerLayout from the Drawer
DrawerLayout drawerLayout = result.getDrawerLayout();
//do whatever you want with the Drawer. Like locking it. 
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

Also try the other lock modes defined here if LOCK_MODE_CLOSED doesn't work and let us know what happens!

Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
0

You can create view for your drawer like this way,try to use ActionsContentView

<shared.ui.actionscontentview.ActionsContentView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/divider"
        app:actions_layout="@layout/actions"
        app:actions_spacing="0dp"
        app:content_layout="@layout/content"
        app:shadow_drawable="@drawable/shadow"
        app:shadow_width="8dip"
        app:spacing="64dip"
        app:spacing_type="right_offset" />

OR try to use Partial SlidingPaneLayout

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96