mikepenz/MaterialDrawer

How to add a button to a profile?

Vanek1756 opened this issue · 13 comments

About this issue

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

  • Briefly describe the issue
  • How can the issue be reproduced / sample code

Details

  • [9.0.0-a03 ] Used library version
  •  Used support library version
  • [ 7.0.3] Used gradle build tools version
  • [ 2020.3.1 patch 2] Used tooling / Android Studio version
  •  Other used libraries, potential conflicting libraries

Checklist

@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.

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

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

Thank you, I will try to do as you said

How can I attach my layout to AccountHeaderView?

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

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

@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

image

this piece of code is written in with (slider )

@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)

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

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

Thanks, you helped me a lot