i'm using a relative layout with a couple of custom views. Each custom view is a collored square, drawn on an own canvas. I want to add a clicklistener to each custom view. Now when adding all Views(with clicklistener) to the layout , only the last custom view added is clickable even when clicking beside the square. Perhaps the problem is that the canvas of this View is overlapping the other canvases. I can see that by setting the backround of each view black. --> only the last view is visible with black background. how can i avoid that problem (without using ontouch and coordinate request) how can i remove that transparent canvas which i am not using.
edit:
public class StorageView extends View {
Paint paintedStorage = new Paint();
int sizeX,sizeY,koordX,koordY;
Storage storage;
public StorageView(Context context, Storage storage, double ratio) {
super(context);
this.koordX = (int)(storage.getKoordX()*ratio);
this.koordY = (int)(storage.getKoordY()*ratio);
this.sizeX = (int)(storage.getSizeX()*ratio);
this.sizeY = (int)(storage.getSizeY()*ratio);
this.storage = storage;
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
if(storage.getArticleList().isEmpty())
paintedStorage.setColor(Color.LTGRAY);
else
paintedStorage.setColor(Color.CYAN);
paintedStorage.setStrokeWidth(3);
canvas.drawRect(koordX, koordY, koordX+sizeX, koordY+sizeY, paintedStorage);
}
public Storage getStorage(){
return storage;
}
This in my Activity: (StorageView is my square or rectangle)
for (int i = 0; i < storageList.size(); i++) {
storageListView.add(new StorageView(context, storageList.get(i), ratio));
// storageListView.get(i).setBackgroundColor(Color.BLACK);
storageListView.get(i).setClickable(true);
storageListView.get(i).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {/*Do something (Toast)*/}});
relativeKoordLayout.addView(storageListView.get(i));
}
edit: i got it "Set the absolute position of a view" worked out fine!