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 add a button to a profile? #2761

Closed Vanek1756 closed 2 years ago

Vanek1756 commented 2 years ago

About this issue

How to add a button to a profile? Can you please provide an example? Screenshot_1

Details

Checklist

mikepenz commented 2 years ago

@Vanek1756 this is not directly supported.

While it is possible to overwrite the default layouts with custom layouts, it will require to be handled with care as the layouts content require to have all original views. Additionally adding listeners to react on the click may not be as simple.

There have been more elaborate answers to similar questions in the past in previous opened issues here.

Vanek1756 commented 2 years ago

I couldn't find more detailed answers. How exactly can I overwrite the default layouts with custom layouts? I would really appreciate your help in solving my problem

mikepenz commented 2 years ago

Depending on the variant you use it will either use this layout: https://github.com/mikepenz/MaterialDrawer/blob/develop/materialdrawer/src/main/res/layout/material_drawer_compact_header.xml Or this layout: https://github.com/mikepenz/MaterialDrawer/blob/develop/materialdrawer/src/main/res/layout/material_drawer_header.xml

You can copy paste this layout over to your project. and add it with the exact same name to your layouts. Then you can apply slight modifications to it. note that general hierarchy and ids must stay the same. mostly add an additional view matching your needs

After that you should be able to find that view you added via the AccountHeaderView in the drawer, and attach the listener you need to it

Vanek1756 commented 2 years ago

Thank you, I will try to do as you said

Vanek1756 commented 2 years ago

How can I attach my layout to AccountHeaderView?

mikepenz commented 2 years ago

Just make sure the xml you have in the project has the exact name as from the library. The build system will automatically overwrite it

Vanek1756 commented 2 years ago

Thanks, you helped me a lot. But I ran into a problem (even before rewriting the layout), when declaring a listener to open the navigation drawer, drawerLayout = null, from which in the future, the listener does not work

mikepenz commented 2 years ago

@Vanek1756 not sure about that part.

Could you please provide a sample or more code to showcase how you setup the library?

The slider is required to be a child of the DrawerLayout: https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/res/layout/activity_sample.xml#L41-L46

The drawerLayout is automatically resolved by using the parent view: https://github.com/mikepenz/MaterialDrawer/blob/develop/materialdrawer/src/main/java/com/mikepenz/materialdrawer/widget/MaterialDrawerSliderView.kt#L481-L489

Vanek1756 commented 2 years ago

image

this piece of code is written in with (slider )

mikepenz commented 2 years ago

@Vanek1756 the provided screenshot sadly does not show how you setup the drawer.

How the XML is structured, and how the view is attached (the drawerLayout will only get defined when the view is attached)

Vanek1756 commented 2 years ago

My XML:

`<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView
        android:id="@+id/slider"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" />

</androidx.drawerlayout.widget.DrawerLayout>`

also i use view binding

after which I initialize the drawer in the code with view binding and add items to it

mikepenz commented 2 years ago

@Vanek1756 you should access the DrawerLayout directly from your ViewBinding not via the MaterialDrawerSliderView.

Vanek1756 commented 2 years ago

Thanks, you helped me a lot