Search code examples
androidimageviewandroid-relativelayout

How to clear child layout in android?


I am developing an android application where I am creating dynamic Images arrow on relative layout. The images are created on a x,y coordinated of the click area of relative layout. Below is the code the I am using for it.

presciptionScreenArrowImg.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (canSelectMedianStatus == 2) {
                if (event == simulationEvent)
                    return false;
                int action = event.getAction();
                int x = (int) event.getX();
                int y = (int) event.getY();
                Log.e("onTouchListener", "User touch at X:" + x + " Y:" + y);
                pointerArrow = new ImageView(getApplication());
                pointerArrow.setImageResource(R.drawable.pointer);
                pointerArrow.setId(imageArrayTag);
                imageArrayTag++;
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
                params.topMargin = y;
                params.leftMargin = x;
                pointerArrow.setLayoutParams(params);
                presciptionScreenArrowImg.addView(pointerArrow);
                long length = 0;
                if (action == MotionEvent.ACTION_DOWN) {
                 //                    click(v, x, y);
                }
            }
            return false;
        }
    });

Now, I need is there on button click the last Image drawn should remove first. Basically I need an undo functionality to remove Images as LIFO structure.


Solution

  • let consider presciptionScreenArrowImg is your main layout which contains your all views so for removing

    int index=presciptionScreenArrowImg.getChildCount();
    if(index>0)
    presciptionScreenArrowImg.removeViewAt(index-1);
    

    from above get count of child and remove last view if you have any problem let me know