1

Hey so I'm trying to make a simple code that makes the visibility of a layout visible once an image been clicked twice or more. Sadly my code doesn't work, but I don't understand why.

Here's my code -

public class MainActivity extends AppCompatActivity {

ImageView logoIMG;
LinearLayout adminLinear;

int cnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    logoIMG = (ImageView) findViewById(R.id.logo);
    adminLinear = (LinearLayout) findViewById(R.id.adminLinear);


    adminLinear.setVisibility(View.INVISIBLE);
    adminLinear.setEnabled(false);


    while (adminLinear.getVisibility() != View.VISIBLE) {
        logoIMG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cnt >= 2) {
                    adminLinear.setVisibility(View.VISIBLE);
                    adminLinear.setEnabled(true);
                } else {
                    cnt++;
                }
            }
        });
    }
}

}

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Dor Furman
  • 55
  • 1
  • 8

3 Answers3

0

Your code just gets stuck in an infinite loop. Setting an onClickListener once is enough. Remove the loop around setOnClickListener altogether, or at least replace it with a conditional (if).

Actine
  • 2,867
  • 2
  • 25
  • 38
0

"The first click just sets the focus to the Image then the second click actually gets handled as a click. "

try setting android:focusable="false" or true. vice versa

Check this Question

Community
  • 1
  • 1
Cruzer
  • 405
  • 5
  • 13
0

Setting onClickListener in your while loop, will endlessly loop and keep on trying to add new Listener, which will cause issue.

Do following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    logoIMG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cnt >= 2) {
                    adminLinear.setVisibility(View.VISIBLE);
                    adminLinear.setEnabled(true);
                } else {
                    cnt++;
                }
            }
        });
}
Sagar
  • 23,903
  • 4
  • 62
  • 62