1

I am working on an Android project in which I have a navigation drawer which shows a list of GroupSection objects. All the GroupSection objects contain a section icon. The drawer icons are determined by a TypedArray and some android specific syntax to get the resource. I want to replace that with a font-awesome icon. Each groupSection object has a getter groupSection.getSectionIcon(); which returns me the String of icon. I have added the icons in String.xml and have the fontawesome-webfont.ttf in assets directory.

I just cannot figure out how to replace the typed array with font-awesome icon. I tried findViewByID(R.id.awesome_icon).getId(), and some other possibilites. Thank you.

COde :

  public void set(List<RestSection> restSectionList, TypedArray navMenuIcons, Long groupId,
                    int canvasid, int sectionId) {
// Below are default icons
  navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);
  for (RestSection restSection : restSectionList) {

// As you can see I am accessing the icons with there resource id. 
            navDrawerItems.add(new DrawerModel(restSection.getMsectionname(),
                    navMenuIcons.getResourceId(0, -1), restSection.getMsectionid()));
        }

Sample of string.xml :

 <string name="pinterest">&#xf0d2;</string>
    <string name="pinterest_square">&#xf0d3;</string>
    <string name="google_plus_square">&#xf0d4;</string>
    <string name="google_plus">&#xf0d5;</string>
    <string name="money">&#xf0d6;</string>
    <string name="caret_down">&#xf0d7;</string>
    <string name="caret_up">&#xf0d8;</string>
    <string name="caret_left">&#xf0d9;</string>
    <string name="caret_right">&#xf0da;</string>
    <string name="columns">&#xf0db;</string>

As in the for-loop I can directly get the icon String with getter, but setting it is where the issue lies. Any help would be nice. Thank you. :-)

Update

I tried the below code where I would manually set, but I get error for that as well :

Unable to find resource: 2131165836
 android.content.res.Resources$NotFoundException: File  from drawable resource ID #0x7f07028c
  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at android.widget.ImageView.resolveUri(ImageView.java:811)
at android.widget.ImageView.setImageResource(ImageView.java:418)

Code :

for (RestSection restSection : restSectionList) {
            if (restSection.getSectionIcon() != null) {
                DrawerModel drawerModel = new DrawerModel();
                drawerModel.setTitle(restSection.getMsectionname());
                drawerModel.setId(restSection.getMsectionid());
                drawerModel.setIcon(R.string.try_icon);

                navDrawerItems.add(drawerModel);
            } else {
                navDrawerItems.add(new DrawerModel(restSection.getMsectionname(),
                        navMenuIcons.getResourceId(0, -1), restSection.getMsectionid()));
            }
        }
We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

1

Font awesome is text. So if you want use them as icons u need to convert them to drawable.

You can use this TextDrawable example: https://stackoverflow.com/a/8831182/3206226

Community
  • 1
  • 1
Murat Mustafin
  • 1,284
  • 10
  • 17
  • Thank you. Can you elaborate on how I can use it for this particular problem. Also I tried some new code, and posted it, can you kindly check. Thank you. :-) – We are Borg Apr 07 '16 at 08:38
  • can u explain what is DrawerModel? – Murat Mustafin Apr 07 '16 at 08:41
  • It is a navigation-drawer for android I am using. One of its similar implementation for another project is here : http://pastebin.com/850gaEf4 . The pastebin contains the model, the loader and settings for entire navigation-drawer functionality. – We are Borg Apr 07 '16 at 08:43
  • If you have acces to this code, you need to change its implementation a little. It is using your resource ids as image resources and creates ImageViews from that. You need to crete Drawable as in my answer from your R.strings.id and then pass it to ImageView. I hope my answer is clear :) – Murat Mustafin Apr 07 '16 at 08:50
  • Line 244 you can see `icon.setImageResource(drawerModelArrayList.get(position).getIcon());` – Murat Mustafin Apr 07 '16 at 08:51
  • I am sorry, but it is not clear. And as you said, I did pass the ID as R.strings.id in the updated code, that gives resource not found exception. Check my main post. Can you tell me what modifications I would require? – We are Borg Apr 07 '16 at 08:53
  • Yes. I mean at line 244 in pastebin.com/850gaEf4 link. It tries to set string resource as drawable and fails with exception – Murat Mustafin Apr 07 '16 at 08:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108489/discussion-between-we-are-borg-and-murat-mustafin). – We are Borg Apr 07 '16 at 08:57