narahari_arjun narahari_arjun - 4 days ago 4
Android Question

Navigation drawer with listview higlighting two list items for dialog box case

I have a navigation drawer with listview having 7 items but i am experiencing a small problem when i click on the last item called Logout which opens a dialog box .

The problem is when i click on any item which opens an activity the item gets highlighted properly , but when i click on logout , the item previously clicked and the logout item clicked both gets highlighted , i want that the logout item should not get highlighted .

My Code :

public int selected_Drawer_Item = 0;

public class NavArrayAdapter extends ArrayAdapter {

Context context;
ArrayList<String> navList;
int[] images;

public NavArrayAdapter(Context context, ArrayList<String> navList,
int[] images) {
super(context, R.layout.row_item_nav, R.id.txtRow, navList);
this.context = context;
this.navList = navList;
this.images = images;
}

@Override
public View getView(int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_item_nav, parent,
false);

TextView textView = (TextView) view.findViewById(R.id.txtRow);
ImageView imageView = (ImageView)
view.findViewById(R.id.imgNav);
RelativeLayout relativeLayout = (RelativeLayout)
view.findViewById(R.id.rel_lay_row_item);

textView.setText(navList.get(position));
imageView.setImageResource(images[position]);

if (Constants.selected_Drawer_Item == position) {
relativeLayout.setBackgroundColor
(context.getResources().getColor
(R.color.listviewitemselect));

textView.setTextColor(context.getResources().
getColor(R.color.dialogOKtext));
}


return view;
}


}

My Listview onitem click

mDrawerList.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View
view, final int i, long l) {
mDrawerLayout.closeDrawers();
mDrawerLayout.postDelayed(new Runnable() {
@Override
public void run() {
switch (i) {
case 0:
Intent intent = new
Intent(DrawerActivity.this, Home.class);
startActivity(intent);
break;

case 1:
Intent intent1 = new
Intent(DrawerActivity.this,
Add_Account.class);
startActivity(intent1);
break;

...... till case 5 same as above

case 6:
final Dialog dialog = new
Dialog(DrawerActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout.LayoutParams dialogParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater.inflate(R.layout.succes_dialog, null);
TextView dialogheader = (TextView) dislogView.findViewById(R.id.textDialogHeader);
TextView dialogmessage = (TextView) dislogView.findViewById(R.id.textDialogMessage);
TextView btnOk = (TextView) dislogView.findViewById(R.id.textDialogOk);
TextView btnNo = (TextView) dislogView.findViewById(R.id.textDialogNo);
dialogheader.setText("Logout");
dialogmessage.setText("Are you sure you want to logout ?");
btnNo.setVisibility(View.VISIBLE);
btnOk.setText("YES");
btnOk.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
dialog.dismiss();
Intent intent = new Intent(DrawerActivity.this, Login.class);
Bundle bndlanimation = ActivityOptions.makeCustomAnimation(
getBaseContext(), R.anim.open_translate,
R.anim.close_translate).toBundle();
startActivity(intent, bndlanimation);
finish();

}
});

btnNo.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
dialog.dismiss();

}
});

dialog.setContentView(dislogView, dialogParams);

dialog.show();
break;
}
}
}, 300);


}
});

Answer

just update if condition in you adapter, your problem will be resolved.

if (Constants.selected_Drawer_Item == position) {
    relativeLayout.setBackgroundColor
    (context.getResources().getColor
    (R.color.listviewitemselect));

    textView.setTextColor(context.getResources().
    getColor(R.color.dialogOKtext));
}

here change if condition like this

if (Constants.selected_Drawer_Item == position && position != 6) {
    /* Here I assumed that your logout menu option's position is 7th so this will work on position 6th*/
    relativeLayout.setBackgroundColor
    (context.getResources().getColor
    (R.color.listviewitemselect));

    textView.setTextColor(context.getResources().
    getColor(R.color.dialogOKtext));
}

udpate your onItemClick with these lines,

AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View 
    view, final int i, long l) {
    Constants.selected_Drawer_Item = i;
    .........
    mDrawerLayout.closeDrawers();
        mDrawerLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                switch (i) {
                    case 0:
                        .............
                        break;
                }
                mDrawerListAdapter.notifyDataSetChanged();
            }
        }, 300);
}
Comments