zsmb13 / MaterialDrawerKt

A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.
Apache License 2.0
518 stars 41 forks source link

How do I change the color of an icon on click in mini drawer? Also, what is an iicon? #58

Closed prateek-dhanuka closed 7 years ago

zsmb13 commented 7 years ago

I believe you're looking for the selectedIconColor(Res) property.

primaryItem("Home") {
    iconColor = 0xFFFF00FF          // Pink
    selectedIconColor = 0xFFFFFF00  // Yellow
}

Note that if you use hardcoded numbers, they should be in ARGB form.


IIcon is an interface that the icons in the mikepenz/Android-Iconincs library implement. That library provides easy to use icons (from these fonts) for your app. You can use it with the Kotlin DSL like this:

secondaryItem("GitHub") { 
    iicon = FontAwesome.Icon.faw_github 
}

For more examples (they are pretty much the same as this one though), see the code of the sample app.

prateek-dhanuka commented 7 years ago

I doesn't seem to work, I'm using it something like,

primaryItem(R.string.drawer_item_my_portofolio) {
      icon = R.drawable.ic_person_black
      iconColor = 0xFFFFFFFF
      selectedIconColor = 0xFF0000F0
}

where, ic_person_black is

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
</vector>

The icon color doesn't change, it stays black. What do I change to make it work?

zsmb13 commented 7 years ago

Based on this answer from the original library, you need to set the iconTintingEnabled attribute as well if you're using a drawable (and not an icon from the Iconics library) that has to be tinted:

primaryItem(R.string.drawer_item_home) {
    icon = R.drawable.ic_person_black
    iconColor = 0xFFFF00FF          // Pink
    selectedIconColor = 0xFFFFFF00  // Yellow
    iconTintingEnabled = true
}

I tried this, and it worked in the sample app.

prateek-dhanuka commented 7 years ago

Thank you very much, It works. But, if I may add to this issue, I need help with something else. The onClick method doesn't work in my application for some reason. It throws this error:

E/AndroidRuntime: FATAL EXCEPTION: main  Process: com.example.prateek.rand0, PID: 9528
   java.lang.IllegalStateException: view must not be null
     at co.zsmb.materialdrawerkt.draweritems.base.AbstractDrawerItemKt$onClick$1.onItemClick(AbstractDrawerItemKt.kt:83)
     at com.mikepenz.materialdrawer.Drawer.setSelectionAtPosition(Drawer.java:579)
     at com.mikepenz.materialdrawer.Drawer.setSelection(Drawer.java:549)
     at com.mikepenz.materialdrawer.MiniDrawer$1.onClick(MiniDrawer.java:503)
     at com.mikepenz.materialdrawer.MiniDrawer$1.onClick(MiniDrawer.java:484)
     at com.mikepenz.fastadapter.FastAdapter$1.onClick(FastAdapter.java:541)
     at com.mikepenz.fastadapter.utils.EventHookUtil$1.onClick(EventHookUtil.java:72)
     at android.view.View.performClick(View.java:5610)
     at android.view.View$PerformClick.run(View.java:22265)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Edit: I traced the source to be com.mikepenz.materialdrawer.Drawer.setSelectionAtPosition(Drawer.java:575-585) which is

if (fireOnClick && position >= 0){
    val item = mDrawerBuilder.mAdapter.getItem(position)

    if (item is AbstractDrawerItem<*, *> && (item as AbstractDrawerItem<*, *>).getOnDrawerItemClickListener() != null) {
        (item as AbstractDrawerItem<*, *>).getOnDrawerItemClickListener().onItemClick(null, position, item)
    }

    if (mDrawerBuilder.mOnDrawerItemClickListener != null) {
        mDrawerBuilder.mOnDrawerItemClickListener.onItemClick(null, position, item)
    }
}

Here, view is being passed null which kotlin doesn't allow, causing an error. How do I resolve this (whch I think means looking for a way to make mDrawerBuilder.mOnDrawerItemClickListener == null?)

I also tried using the onItemClickListener, which threw a rather similar error, but I was able to trace it down to com.mikepenz.materialdrawer.Drawer.SetSelectionAtPosition (Drawer: 582-584) where

if (mDrawerBuilder.mOnDrawerItemClickListener != null) {
    mDrawerBuilder.mOnDrawerItemClickListener.onItemClick(null, position, item)
}

is used, which is quite similar to onClick method.

zsmb13 commented 7 years ago

I've opened a new issue for this bug. Thanks for tracking down the cause as well, made it really easy to fix! I'll make a new release with the fix soon, it should be up in a couple hours tops.

zsmb13 commented 7 years ago

1.1.1 is now up on jcenter, and it hopefully fixes your issues.

prateek-dhanuka commented 7 years ago

I'm getting this error when I'm trying to build my app,

AGPBI: {"kind":"error","text":"Error converting bytecode to dex:\nCause: com.android.dex.DexException: Multiple dex files define Lco/zsmb/materialdrawerkt/draweritems/SectionDrawerItemKtKt$sectionHeader$1;","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexException: Multiple dex files define Lco/zsmb/materialdrawerkt/draweritems/SectionDrawerItemKtKt$sectionHeader$1;\n","tool":"Dex"}
AGPBI: {"kind":"error","text":"com.android.dex.DexException: Multiple dex files define Lco/zsmb/materialdrawerkt/draweritems/SectionDrawerItemKtKt$sectionHeader$1;","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:608)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:563)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:545)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:167)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:194)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:60)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)","sources":[{}]}
zsmb13 commented 7 years ago

This looks like something that would happen when you have different version of this library in your project at the same time.

  1. Do you have multiple modules that include this library? If you do, did you update the version in both of them?
  2. Did you try cleaning your project (both from the menu and deleting the build folders), and then building it?