mikepenz / MaterialDrawer

The flexible, easy to use, all in one drawer library for your Android project. Now brand new with material 2 design.
https://mikepenz.dev
Apache License 2.0
11.67k stars 2.05k forks source link

How to substitute android studios' drawer with MaterialDrawer #1855

Closed fermatijoe closed 7 years ago

fermatijoe commented 7 years ago

Hello, I have created a navigation drawer activity with Android Studio but now I would like to implement MaterialDrawer instead of the existing Drawer because i need drop-down lists. What I'm trying to obtain a is a very stock-like drawer with two main labels and a third one which is an expandable list.

I added the dependency to gradle but I can't manage to replace the old drawer without crashes. Thanks.

My MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    new DrawerBuilder().withActivity(this).build();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setCheckedItem(R.id.newest);

    launchListFragment(methods.get(0));

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.action_search);
    mMaterialSearchView.setMenuItem(item);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if(id == R.id.action_search){
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.newest) {
        launchListFragment(methods.get(0));
    } else if (id == R.id.highest_rated) {
        launchListFragment(methods.get(1));
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

`

And activity_main.xml


        <android.support.v4.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        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="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

`

mikepenz commented 7 years ago

Either you use the MaterialDrawer or the NavigationView you can't use both

mikepenz commented 7 years ago

The MaterialDrawer simplifies most tasks for you. You do not have to define the DrawerLayout or anything special in the layout just check out the samples ;)

fermatijoe commented 7 years ago

Hi, thanks for the answer. I don't understand very much of the samples. I have an issue: why is the hambuerger icon gone? And should the drawer be just an empty grey area? I just used this line: new DrawerBuilder().withActivity(this).build();

mikepenz commented 7 years ago

@fermatijoe just check out the source code of the sample app. It's very simply java code.

Also read the README.

yes. That's an empty drawer if you do not provide items.

fermatijoe commented 7 years ago

And for the hamburger icon? I am trying now with this version of the code: new DrawerBuilder() .withActivity(this) .withTranslucentStatusBar(false) .withActionBarDrawerToggle(false) .addDrawerItems( //pass your items here ) .build();

Since my MainActivity xml has appbarlayout and toolbar but I still dont see the icon

fermatijoe commented 7 years ago

I fixed it with getSupportActionBar().setDisplayHomeAsUpEnabled(true);, you can close this issue, thanks.

russelg1 commented 3 months ago

I am converting an existing implementation of MaterialDrawer too Kotlin Multi Platform.. When installed, I get the DrawerBuilder import is unresolved. Is MaterialDrawer supported in Kotlin MultiPlatform? Are there any specific examples out there or any known changes to gradle that are needed?

mikepenz commented 3 months ago

@russelg1 the library is not multiplatform-compatible. As this library depends on the Android View system, which is Android only.

All it's dependencies are not mulitplatform compatible, and there are no plans to make it multiplatform.


Based on the e-mail you also sent it appears to correctly only define this lib for the androidMain sourceSet. Also make sure you only use it then from the androidMain source set in the project. it won't be available for any other target like commonMain, ...

russelg1 commented 3 months ago

Thanks. Guess I will need to move forward with a Compose alternative

On Mon, Jul 29, 2024, 1:27 AM Mike Penz @.***> wrote:

@russelg1 https://github.com/russelg1 the library is not multiplatform-compatible. As this library depends on the Android View system, which is Android only.

All it's dependencies are not mulitplatform compatible, and there are no plans to make it multiplatform.

— Reply to this email directly, view it on GitHub https://github.com/mikepenz/MaterialDrawer/issues/1855#issuecomment-2255043894, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGBGZ5BJQ6W4XQJXLNHWHUTZOXOGPAVCNFSM6AAAAABLS5CYB2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENJVGA2DGOBZGQ . You are receiving this because you were mentioned.Message ID: @.***>

mikepenz commented 3 months ago

Absolutely. If you plan to go multiplatform and want to share UI code, then Compose multiplatform is the way to go. If you want to have platform-specific UI and only share business logic with other platforms you may still use this lib.

The problem appears to be though that (I assume) you were using the DrawerBuilder in the common sourceSet where it won't work.