-1

I have three views which visibility is currently set to view.setVisibility(view.GONE) and I have a button which I want to change the visibilities to visible one at a time as the user keeps clicking the button. I think I have to use a for loop centred on the button click but I don't know how. Any ideas will be appreciated.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Roach
  • 610
  • 1
  • 8
  • 13

3 Answers3

0

You have to create an array with your views and a variable to know which view is now visible:

View[] views; // put your 3 views inside (0 will be first shown)
int position = 3;

Inside button listener simply change the views visibility:

NOTE the use of static View.GONE and View.VISIBLE

// hide the view
views[position].setVisibility(View.GONE);
// change the position
position = position == 3 ? 0 : position++; 
// show new view
views[position].setVisibility(View.VISIBLE);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0
int i = 0;

onClick(View v){
   switch(++i){
       case 1:
         view1.setVisibility(View.GONE);
         break;
       case 2:
         view2 setVisibility(View.GONE);
         break;
       case 3:
         view3.setVisibility(View.GONE);
         i = 0;
         break;
      }
    }
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

maintain a flag int clickEventFlag = 0

on your button click use

clickEventFlag++;

switch(clickEventFlag % 3){
    case 0:
         //set visible first view
         break;
    case 1:
         //set visible second view
         break;
    case 2:
         //set visible third view
         break;

 }
Kunu
  • 5,078
  • 6
  • 33
  • 61
  • yes but you accept that as a solution, so you can modify that instead of downvoting – Kunu Apr 26 '15 at 10:35
  • nope, I CAN'T modify your code, but I don't downvoted because is ugly I did it because you didn't hide the views in any moment, so after 2 clicks you will see the three views... – Jordi Castilla Apr 26 '15 at 10:37
  • also, downvote a valid answer because of your rage is really according to SO rules ;) – Jordi Castilla Apr 26 '15 at 10:38
  • Sir, Due to "switch(clickEventFlag % 3)" you have to use: if(view.getVisibility() != View.VISIBLE){ view.setVisibility(View.VISIBLE); } – Md. Sajedul Karim Apr 26 '15 at 10:49
  • 1
    @Md.SajedulKarim Actually I was not concern about view visible or gone. I was just trying to show him how he can achieve what he wants. Means in that switch case block he can use his logic to achieve what he wants. – Kunu Apr 26 '15 at 11:01
  • @BojanKseneman your answer was perfect. I am using it although, somehow it has been deleted. Kunu your answer works good but it is based on remainder. So I decided to continue using Bojan's answer. Thanks everybody. So much. – Roach Apr 26 '15 at 11:17