Place Textviews inside Gridlayout at Random position

Viewed 36

I've made a 2 by 2 gridlayout and placed 4 textviews inside them. I've been doing a lot of googling and found out to make them place at random positions first I need to remove all the textviews and then add the views again at random "INDEX". So I created a random variable that generates random numbers from 1 to 4 assuming index is cells inside the gridlayout but I'm getting error "java.lang.IndexOutOfBoundsException: index=2 count=0" which means index count is 0. so how do I add my views at random position inside gridlayout

        int first = r.nextInt(20 - 1 + 1) + 1;
        int second = r.nextInt(20 - 1 + 1) + 1;
        int rand = r.nextInt(4 - 1 + 1) + 1;

        optionLayout.removeAllViews();
        optionLayout.addView(rand1, rand); 
0 Answers
Related