Issue
I am using template from AndroidHive: Material Design and decide to add some icon on each navigation drawer row. I already modified the model NavDrawerItem, getter and setter for icon was added.
// Constructor
public NavDrawerItem(boolean showNotify, String title, int icon) {
this.showNotify = showNotify;
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public int getIcon(){
return icon;
}
public void setTitle(String title) {
this.title = title;
}
public void setIcon(int icon){
this.icon = icon;
}
Also its adapter (NavigationDrawerAdapter) was modified, adding icon support
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
holder.icon.setImageResource(current.getIcon());
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon_drawer);
}
}
I know this drawer was added on onCreate method of FragmentDrawer.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// drawer labels
titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
icons = getActivity().getResources().getIntArray(R.array.nav_drawer_icon);
}
This is my array from string.xml
<string-array name="nav_drawer_labels">
<item>@string/nav_item_home</item>
<item>@string/nav_item_data_visualization</item>
<item>@string/nav_item_pending_item</item>
<item>@string/nav_item_my_profile</item>
<item>@string/nav_item_about_us</item>
</string-array>
<string-array name="nav_drawer_icon">
<item>@drawable/ic_home</item>
<item>@drawable/ic_data_visualization</item>
<item>@drawable/ic_pending_item</item>
<item>@drawable/ic_profile_drawer</item>
<item>@drawable/ic_about</item>
</string-array>
And my navigation drawer item was populated from resource using this piece of code in FragmentDrawer.java
public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
System.out.println(titles[i]);
navItem.setIcon(icons[i]);
System.out.println(icons[i]);
data.add(navItem);
}
return data;
}
My problem is populating icon from resource, in the code above System.out.println(icons[i]); always give me 0 value. I think thats why I can't set imageResource of my navigation drawer icon.
Any help would be appreciated. Thank you.
Solution
Problem is in line:
icons = getActivity().getResources().getIntArray(R.array.nav_drawer_icon);
To read resources id's from array you can use this code:
icons = new int[titles.length];
TypedArray iconsArray = getResources().obtainTypedArray(R.array.nav_drawer_icon);
for (int i = 0; i < icons.length; i++)
icons[i] = iconsArray.getResourceId(i, -1);
iconsArray.recycle();
But probably the best action will be to use NavigationView.
Answered By - Kamil W
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.