Search code examples
androidandroid-relativelayout

Adding ImageView to RelativeLayout dynamically : Not Visible


I am adding ImageView to relative layout dynamically in my code.
But this imageView is sometime visible and sometimes not. I could see while debugging that image is getting added to the layout.

i even used invalidate(); refreshDrawable(); but for no avail.

any idea or help will be highly appreciated.

Firstly, i am removing previously added image to relative layout..

int size=   lv_header.getChildCount();
        for(int i=size;i>=0;i--)
        {
            if(lv_header.getChildAt(i) instanceof ImageView)
                {   if(lv_header.getChildAt(i)!=null){
                            lv_header.removeViewAt(i);
                        break;
                    }

            }

        }

and afterwards, i am adding new imageView..

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,v.getId());

            lv_header.addView(imageButton,lp);
            imageButton.refreshDrawableState();
            lv_header.invalidate();

Solution

  • Looks like you're replacing one ImageView with another? Why not just keep the original ImageView and change it's drawable source with setImageDrawable() or setImageBitmap() ?