android - Stay highlighted the selected item in navigation drawer -


i manually made navigation drawer using actionbardrawertoggle , drawerlayout, want selected item stay highlighted, either when open drawer or close selected item(fragment) should highlighted using color. have listview in drawer. drawer.xml (fragment)

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#34344d"     android:orientation="vertical" >         <listview         android:id="@+id/drawerlist_1"         android:dividerheight="0dp"         android:divider="#fffff7"         android:layout_weight="1"         android:listselector="@drawable/list_view_scolor"         android:layout_width="fill_parent"         android:layout_height="0dp" >     </listview> </linearlayout> 

i have listselector file list_view_scolor:

<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android">      <!-- non focused states -->     <item         android:state_focused="false"         android:state_selected="false"         android:state_pressed="false"         android:drawable="@color/default_color" />     <item         android:state_focused="false"         android:state_selected="true"         android:state_pressed="false"         android:drawable="@color/default_color" />       <!-- focused states --> <item android:state_focused="true"       android:state_selected="true"       android:state_pressed="false"       android:drawable="@drawable/list_view_listselector" />           <item         android:state_focused="true"         android:state_selected="false"         android:state_pressed="false"         android:drawable="@drawable/list_view_listselector" /> <!-- pressed --> <item         android:state_pressed="true"         android:drawable="@drawable/list_view_listselector" /> </selector> 

i have list_view_listselector.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle">      <solid android:color="@color/pressed_color" />  </shape> 

i have not posted code of single_row, if need see file, can update that. please comment if required.

baseadapter listview :-

public class listview_adapter extends baseadapter {      context context;     string[] list;     layoutinflater inflater;     public listview_adapter(context context , string[] list ) {         this.context = context;         this.list = list;         inflater =(layoutinflater) context.getsystemservice(context.layout_inflater_service);     }      @override     public int getcount() {         // todo auto-generated method stub         return list.length;     }      @override     public object getitem(int position) {         // todo auto-generated method stub         return null;     }      @override     public long getitemid(int position) {         // todo auto-generated method stub         return 0;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         view view = convertview;          view=   inflater.inflate(r.layout.cust_row_listview, null);          textview drawer_item = (textview) view.findviewbyid(r.id.drawer_item);         string item = list[position];         drawer_item.settext(item);         typeface type = typeface.createfromasset(context.getassets(),                 "robot_condensed_light.ttf");         drawer_item.settypeface(type);     /*  if(position==0){             view.setbackgroundcolor(color.parsecolor(context.getresources().getstring(r.string.color_list_1)));         }         else if(position == 1){             view.setbackgroundcolor(color.parsecolor(context.getresources().getstring(r.string.color_list_2)));         }         else if(position == 2){             view.setbackgroundcolor(color.parsecolor(context.getresources().getstring(r.string.color_list_3)));         }         else if(position == 3){             view.setbackgroundcolor(color.parsecolor(context.getresources().getstring(r.string.color_list_4)));         }         else if(position == 4){             view.setbackgroundcolor(color.parsecolor(context.getresources().getstring(r.string.color_list_5)));         }*/         return view;     }   } 

this implementation of how navigate through fragments

lv = (listview) findviewbyid(r.id.drawerlist_1);         listview_adapter adapter1 = new listview_adapter(this, item1);         lv.setadapter(adapter1);         fragment_home frag = new fragment_home();         fragmanager = getsupportfragmentmanager();         fragmanager.begintransaction().replace(r.id.framelayout, frag).commit();         lv.setonitemclicklistener(new onitemclicklistener() {              @override             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 //lv.setitemchecked(position, true);                 //v.setselection(position);                 lv.setselected(true);                 fragment frag;                 if (position == 0) {                     // getsupportactionbar().setbackgrounddrawable(new                     // colordrawable(getresources().getcolor(r.string.color_list_2)));                     getsupportactionbar().settitle("home");                     frag = new fragment_home();                     fragmanager.begintransaction()                             .replace(r.id.framelayout, frag).commit();                 } else if (position == 1) {                     // getsupportactionbar().setbackgrounddrawable(new                     // colordrawable(color.parsecolor(getresources().getstring(r.string.color_list_2))));                     frag = new fragment_paytmwallet();                     fragmanager.begintransaction()                             .replace(r.id.framelayout, frag).commit();                     getsupportactionbar().settitle("paytm wallet");                     // if(build.version.sdk_int>=21){                     // window window = getwindow();                     // window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);                     // window.clearflags(windowmanager.layoutparams.flag_translucent_status);                     // window.setstatusbarcolor(color.parsecolor(getresources().getstring(r.string.color_list_primary_2)));                     // }                 } else if (position == 2) {                     // getsupportactionbar().setbackgrounddrawable(new                     // colordrawable(color.parsecolor(getresources().getstring(r.string.color_list_3))));                     frag = new fragment_categories();                     fragmanager.begintransaction()                             .replace(r.id.framelayout, frag).commit();                     getsupportactionbar().settitle("categories");                     // if(build.version.sdk_int>=21){                     // window window = getwindow();                     // window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);                     // window.setstatusbarcolor(color.parsecolor(getresources().getstring(r.string.color_list_primary_3)));                     // }                 } else if (position == 3) {                     // getsupportactionbar().setbackgrounddrawable(new                     // colordrawable(color.parsecolor(getresources().getstring(r.string.color_list_4))));                     frag = new fragment_askaques();                     fragmanager.begintransaction()                             .replace(r.id.framelayout, frag).commit();                     getsupportactionbar().settitle("ask question");                     // if(build.version.sdk_int>=21){                     // window window = getwindow();                     // window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);                     // window.setstatusbarcolor(color.parsecolor(getresources().getstring(r.string.color_list_primary_4)));                     // }                 } else if (position == 4) {                     // getsupportactionbar().setbackgrounddrawable(new                     // colordrawable(color.parsecolor(getresources().getstring(r.string.color_list_5))));                     frag = new fragment_beanexpert();                     fragmanager.begintransaction()                             .replace(r.id.framelayout, frag).commit();                     getsupportactionbar().settitle("be expert");                     // if(build.version.sdk_int>=21){                     // window window = getwindow();                     // window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);                     // window.setstatusbarcolor(color.parsecolor(getresources().getstring(r.string.color_list_primary_5)));                     // }                 }                  dl.closedrawers();              }         }); 

follow steps below.

in adapter create variable below , getter function.

int mselecteditem;  public void setselecteditem(int selecteditem) {     this.mselecteditem = selecteditem;     notifydatasetchanged();   } 

under getview method include below.

if (position == mselecteditem) {       drawer_item.settextcolor.setbackgroundcolor(contextcompat.getcolor(context, r.color.selected_color)); // highlighting color     } else {       drawer_item.settextcolor(contextcompat.getcolor(context, android.r.color.normal_color)); // normal color     } 

in mainactivity call setselecteditem passing position below under onitemclick.

adapter.setselecteditem(position); 

by have changed highlighted text color. can follow same set background. check if works you.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -