0

I am using a custom single choice listview. I want to call ItemOnClickListener programmatically. I am using android:listSelector="#47D149" property in my xml. I saw this post. performItemClick function is working but doesn't change the background of list item.

What should I do? Any suggestions. Thanks!

5
  • So you want the item selected programmatically?Or do you want the item to remain selected once you have the item? Commented Feb 25, 2015 at 13:49
  • Yes I want the item selected programmatically. But selected item background is not painted. @Droidekas Commented Feb 25, 2015 at 13:56
  • Well what i normally do is put the background color logic in my adapter to indicate selected item.Is that too much of a change for you? Commented Feb 25, 2015 at 14:03
  • @Droidekas Yes it works but xmls which have colors shouldn't inside "<selector></selector>" tag. I was trying to do in "<selector></selector>" tags all day. So, they should be in Shifar Shifz answer. Now Should I make green Shifar Shifz answer? Commented Feb 25, 2015 at 14:33
  • if his answer solves your problem. then yes you should :) Commented Feb 25, 2015 at 14:41

3 Answers 3

1

SAMPLE

ListView

<ListView
            android:id="@+id/navigation_menu_container"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_below="@id/rlBanner"
            android:layout_gravity="start"
            android:background="@color/nyc_black"
            android:choiceMode="singleChoice"
            android:divider="@color/border_black"
            android:dividerHeight="@dimen/divider_height"
            android:listSelector="@drawable/item_selector" >

drawables/item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_activated="false" android:drawable="@drawable/item_normal"></item>
    <item android:state_pressed="true" android:drawable="@drawable/item_pressed"></item>
    <item android:state_activated="true" android:drawable="@drawable/item_pressed"></item>
</selector>

drawables/item_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="@color/black"
        />
</shape>

drawables/item_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
     <solid 
         android:color="@color/red"
         />
</shape>

Try the above sample. :) tell me if it works

Sign up to request clarification or add additional context in comments.

5 Comments

I dont have selector xml, list selector is a color
Sorry but no, performItemClickfunction worked but the listview item ui not changed
Set android:listSelector="@drawable/item_selector" as custom listview row background
@kalidossrajendran sorry I see after I answer my own the question. But this is the answer. Thank you
should I select your answer as correct answer ? @ShifarShifz
0

Ok. I have found the solution. :) @Shifar Shifz answer, dont use

android:listSelector="@drawable/item_selector"

Instead of it, add this drawable xml to your row.xml layout as background

android:background="@drawable/item_selector"

Comments

0

Maybe you could just take care of it in your adapter itself?

@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (null == convertView) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        if(this.getItem(position).isChecked()){
            convertView.setBackgroundResource(android.R.color.holo_blue_bright);
        }else{
            convertView.setBackgroundResource(android.R.color.white);
        }

        final View tempFinalView = convertView;
        tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                              @Override
                                                              public void onClick(View v) {
                                                                  if (getItem(position).isChecked()) {
                                                                      getItem(position).setChecked(false);
                                                                      tempFinalView.setBackgroundResource(R.color.white);
                                                                  } else {
                                                                      getItem(position).setChecked(true);
                                                                      tempFinalView.setBackgroundResource(R.color.pressed);
                                                                  }
                                                              }
                                                          }
        );

   return convertView;
}

EDIT: If you dont want to add another variable to the item.You could use the tag feature instead.

Just set and get the tag.

private static int VIEW_TAG=99;

@Override public View getView(final int position, View convertView, final ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, parent, false); }

    if((Boolean)convertView.getTag(VIEW_TAG)){
        convertView.setBackgroundResource(android.R.color.holo_blue_bright);
    }else{
        convertView.setBackgroundResource(android.R.color.white);
    }

    final View tempFinalView = convertView;
    tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                          @Override
                                                          public void onClick(View v) {
                                                              if (getItem(position).isChecked()) {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(false));
                                                                  tempFinalView.setBackgroundResource(R.color.white);
                                                              } else {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(true));
                                                                  tempFinalView.setBackgroundResource(R.color.pressed);
                                                              }
                                                          }
                                                      }
    );

return convertView; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.