views:

141

answers:

1

My app's underlying view has bitmaps, lines, etc drawn on a canvas. I want to display images (icons) on top of this which will respond to Touch events. The number and positioning of such images will be variable depending on data in an SQLite database.

Am I correct in thinking that the way to achieve this is to have a ViewGroup to which I add first the basic view then for each icon an ImageButton object with an OnTouch listener and positioning defined by setting the margins in a RelativeLayout.LayoutParams object?

My code for each button reads as follows: `

public class MyButton {

ImageButton mButton;
private Bitmap Star;

public MyButton(int intX, int intY) {
    mButton = new ImageButton(Global.thisApp);     
    mButton.setImageBitmap(BitmapFactory.decodeResource(Global.thisApp.getResources(), R.drawable.star)r);
    mButton.setPadding(0, 0, 0, 0);
    mButton.setOnTouchListener(new ImageButton.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // ToDo
            return false;
        }
    });
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams 
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(intX, intY, 0, 0);
    Global.thisApp.mLayout.addView(mButton, params); 
}

}

` and the code which creates the buttons is as follows:

      poiCount = 0;
    mCursor=Global.mDBAdapter.fetchItems(...);
    if (mCursor.getCount()>=0) {
        poi = new MyButton[mCursor.getCount()];
        mCursor.moveToFirst();
        do {
            int X = ... ;
            int Y = ... ;
            poi[poiCount] = new MyButton("Type", X, Y);
            poiCount++;
        } while (mCursor.moveToNext());
    }

This code is called from the onDraw() method for the underlying view.

This seems to work when the screen is initially drawn, and when the screen is subsequently redrawn (with new data) new buttons are added; however, the previous buttons are not removed.

I've tried calling Global.ThisApp.MyLayout.removeView with each in turn button as a parameter but Eclipse objects that a MyButton object cannot be a parameter for removeView.

How can I remove the existing buttons each time the underlying view is invalidated?

A: 

MyButton should extend View (ImageButton in your case) to be added to or removed from a ViewGroup.

dtmilano
Thanks. I'll try that tomorrow. Should I delete the mButton field and the first line of the Constructor:(mButton=new ImageButton(Global.thisApp)) ?
Yes, you don't need that line. You constructor should also receive other parameters (Context at least).
dtmilano
I've amended the MyButton class so that it extends ImageButton rather than having ImageButton as a member. This works, but I still seem to have the same problems:- the buttons are slow to appear (on the emulator, at any rate)- they don't respond to touches (I've tried putting a Toast inside the OnTouch method)- they aren't removed when the ViewGroup (a RelativeLayout) is invalidated.Luckily, my alternative code (ignoring ImageButtons and drawing direct to the canvas) seems to work, so I think I'll stick with that. (It makes it easy to detect if a touch is close to more than one icon.)