setFocus(overlayItem) of ItemizedOverlay is not working

I am working on android application and I am able to displayed multiple icons on map using ItemizedOverlay and I have also implemented onTap(int index) method of ItemizedOverlay to display icon specific information in a required window.

Now, I want to change icon of selected overlay when user click on some other coponent. I am calling setFocus(OverlayItem) method of ItemizedOverlay to display different icon of specific overlay. it works fine when user tap on any specific overlay but does not change icon when I call setFocus(OverlayItem) method.

Any pointers? what is best way of programtically changing icon of selected overlay in mapView?

I have overriden getMarker method of my custom OverlayItem class to display different markers for different state of overlayItem. and I want to use setFocus(OverlayItem) method to change the state of OverlayItem and also change the marker when selected.

public Drawable getMarker(int stateBitset){
  icon = Util.getCategoryMapIcon(0);   
  icon.setBounds(0-icon.getIntrinsicWidth()/2, 0-icon.getIntrinsicHeight(), icon.getIntrinsicWidth()/2, 0);
  return icon;
 }else {
  icon = Util.getCategoryMapIcon(OverlayItem.ITEM_STATE_SELECTED_MASK);
  icon.setBounds(0-icon.getIntrinsicWidth()/2, 0-icon.getIntrinsicHeight(), icon.getIntrinsicWidth()/2, 0);
  return icon;   

here Util.getCategoryMapIcon(0) is Utility method to return appropriate icon, this method takes some parameters, which I have removed to make this example look simple.

and below is code to change the state

Button leftNavigation = (Button) findViewById(;
leftNavigation.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
  OverlayItem item = searchResultsOverlay.get(index+1);

Any help will be much appreciated. Thanks, Aamir

Asked by: Tess335 | Posted: 24-01-2022

Answer 1

In your getMarker code, try returning a StateListDrawable (which corresponds to <selector> in drawable XML) that contains a default drawable (no states) and a drawable with the android.R.attr.state_focused state.

See a related question here: Android ListView Selector Color

Answered by: Vanessa997 | Posted: 25-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android