How to make List item highlighted and stay selected

My ListView has a handful items in it.

I’d like to see clicked item highlighted and stay highlighted. I don’t want to take any actions, just to see which item was clicked. User will have opportunity to select action from the action bar menus.

 

Info:

1. OnListItemClick tried to add code setSelected(position). Ha-ha. no effect.

2. set focusable. created xml file with two colors for selection and for background. no effect (oh, background does change but item doesn’t stay selected

3. more reading. Found! I have to “implemen OnClickListener” (or smth) class. And override OnItemClick method.

  prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Object o = prestListView.getItemAtPosition(position);
            String str=(String)o;//As you are using Default String Adapter
            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
        }
    });
This worked. (I have similar code)
Peculiar point: works on device, but not on emulator. On emulator I have i click and drag a little. On a device i have to tap wih a finger, very light tap is fine. 


4. This will be useful for adding image to the list item 
http://stackoverflow.com/questions/4461134/android-listview-adapter-onclicklistener-issue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s