1

I am trying to make a PIN authentication method. I have 9 buttons [1 to 9] to user insert the PIN.

public WindowManager winManager;
public RelativeLayout wrapperView;
public Button button1,button2,button3,button4,button5,button6,button7,button8,button9;
public ArrayList<Integer> PIN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PIN=new ArrayList<>();
    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
    this.wrapperView = new RelativeLayout(getBaseContext());
    getWindow().setAttributes(localLayoutParams);
    View.inflate(this, R.layout.activity_main, this.wrapperView);
    this.winManager.addView(this.wrapperView, localLayoutParams);
    button1 = (Button)wrapperView.findViewById(R.id.button1);
    button2 = (Button)wrapperView.findViewById(R.id.button2);
    button3 = (Button)wrapperView.findViewById(R.id.button3);
    button4 = (Button)wrapperView.findViewById(R.id.button4);
    button5 = (Button)wrapperView.findViewById(R.id.button5);
    button6 = (Button)wrapperView.findViewById(R.id.button6);
    button7 = (Button)wrapperView.findViewById(R.id.button7);
    button8 = (Button)wrapperView.findViewById(R.id.button8);
    button9 = (Button)wrapperView.findViewById(R.id.button9);
    button1.setOnClickListener(mButton1_OnClickListener);
    button2.setOnClickListener(mButton1_OnClickListener);
    button3.setOnClickListener(mButton1_OnClickListener);
    button4.setOnClickListener(mButton1_OnClickListener);
    button5.setOnClickListener(mButton1_OnClickListener);
    button6.setOnClickListener(mButton1_OnClickListener);
    button7.setOnClickListener(mButton1_OnClickListener);
    button8.setOnClickListener(mButton1_OnClickListener);
    button9.setOnClickListener(mButton1_OnClickListener);
}

final View.OnClickListener mButton1_OnClickListener = new View.OnClickListener() {
    public void onClick(final View v){
        switch(v.getId()){
            case R.id.button1:
                PIN.add(1);
            case R.id.button2:
                PIN.add(2);
            case R.id.button3:
                PIN.add(3);
            case R.id.button4:
                PIN.add(4);
            case R.id.button5:
                PIN.add(5);
            case R.id.button6:
                PIN.add(6);
            case R.id.button7:
                PIN.add(7);
            case R.id.button8:
                PIN.add(8);
            case R.id.button9:
                PIN.add(9);
        }
        if (PIN.size() == 4){ //
            winManager.removeView(wrapperView);
            wrapperView.removeAllViews();

        }
    }
};

On click listener I am trying to know when the PIN inserted by the user has four numbers, but it is not entering on if and I don't know why.

tomss
  • 361
  • 3
  • 13

1 Answers1

3

you forgot to add break that's why it's inserting the number of time which matches the case, ie. if you click on button1 it will insert value still case 9 and then PIN.size() = 9

  case R.id.button1:
     PIN.add(1);
     break;

Add break; to each case

Rohit Patil
  • 1,068
  • 8
  • 9
  • 1
    I have no idea how many times I've been programming a `switch`/`case` statement, forgot to put in `break`s, and then wondering why it `broke`. – DDPWNAGE Dec 15 '15 at 20:42