Issue
I have a navigation view that I populate dynamically according to the response I get from an API request. So I have to do most of the menu items design programmatically.
My aim is to add the indicator (yellow vertical strip) in left side when a menu item is checked, as in this picture:
Here is my code up to now:
.xml
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@drawable/drawer_item"
app:itemTextColor="@drawable/drawer_item"
app:itemBackground="@drawable/background_item"/>
.java
for (SettingsMenuItem item : items) {
if (!item.getMenu().isEmpty()) {
span = new SpannableString(subItem.getTitle());
span.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, span.length(), 0);
menu.add(NONE,NONE, items.indexOf(subItem), span).setCheckable(true).setChecked(false);;
}
}
menu.getItem(selectedItemID).setChecked(true);
Solution
Try creating custom view for yellow vertical strip like
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="@color/yellow"/>
and set gravity to parent left. Also maintain the status of clicked menu somewhere in SharedPreference so as to restore the state of clicked menu.
Answered By - Nikhil Lotke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.