0

I have following button in my android activity.

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(b1.isChecked())
                if(b2.isChecked())
                    if(name1.getText().length()!=0 && name2.getText().length()!=0)
                    calculatelove();
        }
    });

on click it calls the following function

private  void calculatelove()
{
    findViewById(R.id.resultLayout).setVisibility(LinearLayout.VISIBLE);
    btn.setEnabled(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Random rand = new Random();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_finger1.setText(name1.getText().toString().toUpperCase());
                        tv_finger2.setText(name2.getText().toString().toUpperCase());
                    }
                });
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress = (((float) (i + 1) / 5) * 100);
                    progressBar.setProgress((int) progress);
                    Animation fadeIn = new AlphaAnimation(0, 1);
                    fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
                    fadeIn.setDuration(500);
                    tv_heart2.startAnimation(fadeIn);
                    tv_progress.setText("Please wait....." + (int) progress + "%");

                }
                tv_progress.setText("Complete");
                tv_progress.setTextColor(Color.rgb(92,184,92));
                tv_heart2.setBackgroundResource(R.drawable.heart);
                tv_heart2.setText(rand.nextInt(100) + "%");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btn.setEnabled(true);
                    }
                });
            }
        }).start();
}

It works fine but app hangs ,if we make multiple clicks on the button. thanks in advance .

If anyone can figure out reason of it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

0 Answers0