I have programatically created a set of ImageButton views (with transparent background) as map pins on a static map (ImageView) defined in my app. At certain locations on the map, there are a few ImageButtons that are quite close to each other. I have noticed that in such situation even I am clicking on button 1, the app will think that button 2 is being clicked on and thus proceed to run the activity associated with button 2. Turning off transparency on these buttons reveals that the buttons are actually much bigger than the associated source images and thus some of these buttons have overlapped.
How do I define the imagebuttons such that they are size to the same size as the source images?
Here is a snapshot of relevant code under onCreate():
ImageButton btnMapLoc = new ImageButton(ResultMapActivity.this);
RelativeLayout.LayoutParams vp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
vp.setMargins(x,y,0,0); // [left, top, right, bottom] - in pixels
btnMapLoc.setLayoutParams(vp);
//btnMapLoc.setBackgroundColor(Color.TRANSPARENT);
btnMapLoc.requestLayout();
String imgNameNormal = "map_loc_" + mapLocation + "_normal"; //e.g. map_loc_10_normal
int idNormal = getResources().getIdentifier(imgNameNormal,"drawable",getPackageName());
String imgNameClick = "map_loc_" + mapLocation + "_click"; //e.g. map_loc_10_click
int idClick = getResources().getIdentifier(imgNameClick,"drawable",getPackageName());
btnMapLoc.setImageResource(idNormal);
rl.addView(btnMapLoc, vp);
// Change image as the state of the button changed (normal vs. clicked)
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {-android.R.attr.state_pressed},getResources().getDrawable(idNormal)); // Note the "-"
states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(idClick));
btnMapLoc.setImageDrawable(states);
Thanks.
Try this, it works for me.