0

In the gridlayout there are nine imageview in which some of the imageview image are drawn in a method.Now I want to remove the already drawn image in imageview in another method.I tried this but the app crashes.

GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

for (int i = 0; i< gridLayout.getChildCount(); i++) {

    ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Sanjeev Jha
  • 71
  • 1
  • 1
  • 7
  • Possible duplicate of [this question](https://stackoverflow.com/questions/2859212/how-to-clear-an-imageview-in-android). If `setImageResource(0)` isn't working for you, I would suggest to use `setImageDrawable(null)` or one of the other answers mentioned there. – shriakhilc Oct 13 '18 at 14:19
  • Possible duplicate of [How to clear an ImageView in Android?](https://stackoverflow.com/questions/2859212/how-to-clear-an-imageview-in-android) – shriakhilc Oct 13 '18 at 14:22

1 Answers1

0

try the following code. the logcat gives this error, since gridlayout is a legacy view, you have to do it the long way around. The code should find the gridlayout view and remove each child's image, thus reseting the grid.

androidx.gridlayout.widget.GridLayout grid = (androidx.gridlayout.widget.GridLayout)findViewById(R.id.gridLayout);

for (int i = 0; i < grid.getChildCount(); i++) {
        ((ImageView)grid.getChildAt(i)).setImageResource(0);
    }
ihashib
  • 1
  • 1